Javascript hex to buffer

Saving hex data to file in NodeJS

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]/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]: , 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) 
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?

Источник

How to convert hex string into a bytes array, and a bytes array in the hex string?

The following code procedure bytes = parseHexString (createHexString (bytes)) leads to updated of bytes, what I would like to avoid. And as a result calculations are not correct.

         

I am not javascript developer, and not one found in google code did not work on this data. Update 1

console.log(createHexString(rsa_privk[0])); = e5d109c673d8ef03df564beb9e36e9983a23842b0a724efa45ff76bbe5ad72ed62d2757968 
parseHexString('e5d109c673d8ef03df564beb9e36e9983a23842b0a724efa45ff76bbe5ad72ed‌​62d2757968'); 
console.log(rsa_privk[0].toString(16)); 

output: 123676133,198914513,129998601,245147334,11918451,206998232,96766191,75984899,177840095,106709334,10180427,208237547,119814814,127003446,189062377,84099480,220452154,250519075,267883908,115471915,165124106,238628722,169382478,42320122,95982405,80725759,89608310,85166267,200925925,254033325,86971506,191278317,127411298,180195794,142776693,188738169,39016 Update 3

console.log(parseHexString(createHexString(rsa_privk[0]))); console.log(rsa_privk[0]); 
[229, 209, 9, 198, 115, 216, 239, 3, 223, 86, 75, 235, 158, 54, 233, 152, 58, 35, 132, 43, 10, 114, 78, 250, 69, 255, 118, 187, 229, 173, 114, 237, 98, 210, 117, 121, 104] [123676133, 198914513, 129998601, 245147334, 11918451, 206998232, 96766191, 75984899, 177840095, 106709334, 10180427, 208237547, 119814814, 127003446, 189062377, 84099480, 220452154, 250519075, 267883908, 115471915, 165124106, 238628722, 169382478, 42320122, 95982405, 80725759, 89608310, 85166267, 200925925, 254033325, 86971506, 191278317, 127411298, 180195794, 142776693, 188738169, 39016] 

Источник

Читайте также:  Ubuntu install python gtk
Оцените статью