Friday 19 May 2017

How to determine the endian (LE or BE) when writing a file to hard disk in NodeJS?

I'm trying to write a buffer as follows:

[0x01, 0x02, 0x03, 0x04]

When I read the resulting file with HexDump, I see:

0000000 0201 0403                              
0000004

Here's the code I'm using:

var fs = require('fs');
var write = fs.createWriteStream('result.txt');
var testBuffer = new Buffer([0x01, 0x02, 0x03, 0x04], 'hex');

for(i = 0; i < testBuffer.length; i++){
    fs.appendFile('resultAppend.txt', new Buffer([testBuffer[i]]), { encoding: null});
}


for(i = 0; i < testBuffer.length; i++){
    console.log(i, testBuffer[i]);
}

console.log(testBuffer)

write.write(testBuffer);

Can you help me figure out how to define the Word Endian when writing the Buffer into a Writable Stream?



via Nico Andrade

No comments:

Post a Comment