Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Buffer- read little endian buffer

Tags:

node.js

buffer

I have a 256-bit long but written as a little endian:

<Buffer 21 a2 bc 03 6d 18 2f 11 f5 5a bd 5c b4 32 a2 7b 22 79 7e 53 9b cb 44 5b 0e 00 00 00 00 00 00 00>

How can I correctly print it as a hexadeciaml value?

buf.toString('hex')

buk.toString('hex').split("").reverse().join("")) gives 0x00000000000000e0b544bcb935e79722b72a234bc5dba55f11f281d630cb2a12 instead of 0x000000000000000e5b44cb9b537e79227ba232b45cbd5af5112f186d03bca221

like image 891
Spearfisher Avatar asked Sep 17 '14 18:09

Spearfisher


People also ask

How do I decode a buffer in Node JS?

In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.

What does the BUF toJSON () method return in node JS?

The toJSON() method returns a JSON object based on the Buffer object.

How do you decode a binary buffer to an image in node JS?

readSync(fd, imageBuffer, 0, size, Address); let imgData = new Blob(binary. buffer, {type: 'application/octet-binary' }); let link = URL. createObjectURL(imgData); data. src = new image(); img.

How do you convert a buffer to a string value?

In a nutshell, it's easy to convert a Buffer object to a string using the toString() method. You'll usually want the default UTF-8 encoding, but it's possible to indicate a different encoding if needed. To convert from a string to a Buffer object, use the static Buffer.


2 Answers

You can use match instead of split to get an array of the two character groups. Then you can reverse the array and join it.

buf.toString('hex').match(/.{2}/g).reverse().join("")
like image 66
yate Avatar answered Oct 17 '22 00:10

yate


Actually Buffer objects support reverse() method, and it may be better to use it before converting to hex string,

buf.reverse().toString('hex')
like image 36
mpapec Avatar answered Oct 17 '22 00:10

mpapec