I'm trying to write 16bit integers to a file, but didn't got the result I was expecting. For example:
>var d = new Uint16Array([255, 1111, 42])
=new Uint16Array([255, 1111, 42])
>var dr = fs.readFile("log0.txt")
\xFF is 255, ok for one byte.
1111 is 0100 0101 0111 in binary, and W is 87 character code, 0101 0111 binary. Looks like the lower byte of the 16 bit integer is saved.
* is 42, ok.
0100 0101 0111
My question is: Looks like saving typed arrays directly is not supported. Will it be, or what would be the best way to save this data as fast and efficient as possible?
Board is a Pixl running 2v08.188 (and of course shouldn't call a binary file .txt)
not solution but maybe workaround or thing to check - you can retype the array as Uint8Array in place (they share same memory) and see how that one gets written or check how it compares to 'bad' file you have.
>var d8 = new Uint8Array(d.buffer)
or even check d.buffer - that will be probably the same byte array as d8 would be
Much better, file length matches the buffer-s length. First couple of bytes match. I think all I need is read back on the computer.
Yeah, the issue is that fs.appendFile("log0.txt", d) (and pretty much any 'write' command) iterates over the given array, assuming each element is a byte (even if you supply a UintXYZArray). It makes more sense if you think of what you'd want if you supplied just a simple array like [1,2,3,4].
All you need to do to write is fs.appendFile("log0.txt", d.buffer)
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
For a full reference visit the Markdown syntax.
© Espruino, powered by microcosm.
Report a problem