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?