Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving hex data to file in NodeJS

Tags:

node.js

fs

hex

Say I've got a string

"020000009B020000C0060000"

and I want to turn it into a string such that if it was saved as a file and opened it in a hex editor it would display the same hex as I put in. How do I do this? I've never had to use buffers or anything, but in trying to do this I've seemingly had to, and I don't really know what I'm doing.

(or whether I should be using buffers at all- I've been searching for hours and all I can find are answers that use buffers so I've just taken their examples and yet nothing is working)

I turned the hex string into a buffer

function hexToBuffer(hex) {
  let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16)
  }))
  return typedArray
}

and this seemed to work because logging hexToBuffer("020000009B020000C0060000").buffer what it returns gave me this:

ArrayBuffer {
  [Uint8Contents]: <02 00 00 00 9b 02 00 00 c0 06 00 00>,
  byteLength: 12
}

which has the same hex as I put in, so it seems to be working fine, then to make the array buffer into a string I did this.

let dataView = new DataView(buffer);
let decoder = new TextDecoder('utf-8');
let string =  decoder.decode(dataView)

just to test that it worked, I saved it to a file.

fs.writeFileSync(__dirname+'/test.txt', string)

Opening test.txt in a hex editor shows different data: 02000000EFBFBD020000EFBFBD060000

If I instead do

fs.writeFileSync(__dirname+'/test.txt', hexToBuffer("020000009B020000C0060000"))

then I get the correct data- but then if I read the file with fs and then add to it it's once again not the same values.

let test = fs.readFileSync(__dirname+'/test.txt', 'utf8)
let example2 = test+'example'
fs.writeFileSync(__dirname+'/test.txt', example2)

now test.txt begins with 02000000EFBFBD020000EFBFBD060000 instead of 020000009B020000C0060000. What do I do?


1 Answers

First of all, you can use the Buffer.from(string[, encoding]) method to create a Buffer from a string whilst also specifying the encoding - in your case it will be "hex":

const b1 = Buffer.from("020000009B020000C0060000", "hex")

Now save it to a file:

fs.writeFileSync(path.resolve('./test'), b1)

Then we can check that the file contains the same hex values as in the string by using xxd on the command line:

$ xxd test
0200 0000 9b02 0000 c006 0000

Looking good!

Now we can read the file back into a buffer as well, making sure to tell it the encoding of the file is "hex" again:

const b2 = fs.readFileSync(path.resolve("./test"), "hex")

Finally, turn it back into a string again with the Buffer.toString() method:

console.log(b2.toString()) // => "020000009b020000c0060000"
like image 141
sdgluck Avatar answered Apr 09 '26 15:04

sdgluck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!