Convert hex in bin

Hi all,

i use in an Node Red function node:

msg.payload = parseInt("0x"+msg.payload);
msg.payload = msg.payload.toString(2).padStart(96, '0');

to convert following hex value: 30347A120002AF4400000001.
the result is not the same with the result generated by an online converter (like RapidTables). It simply differs from position 52 to end...
(the result is expected to have 96 positions).
Question: are there some known limitations? why is the result different ?

Thanks :slight_smile:

When I convert 0x30347A120002AF4400000001 to binary, I get:

> parseInt("0x30347A120002AF4400000001").toString(2)
'1100000011010001111010000100100000000000000010101100000000000000000000000000000000000000000000'

That is the same result as when I enter the same hex value into https://www.rapidtables.com/convert/number/index.html

So I can't say why you are getting a different result. Are you sure you are starting with the same hex value you have shared here? What actual result do you get?

It looks like something's wrong with both results. For a 24 hex digits you should get 96 bits, and from the value, the last digit should be a '1'.

My first guess would be a lack of sufficient digits to handle 96 bit values.

edit : looks like Javascript integer types only handle 64 bits max.

Running in Node Red i get:

{"_msgid":"60671b07.b2be44","topic":"","payload":"1100000011010001111010000100100000000000000010101100000000000000000000000000000000000000000000"}

i am confused ...

https://www.rapidtables.com/convert/number/hex-to-binary.html
give the result (1):

1100000011010001111010000100100000000000000010101011110100010000000000000000000000000000000001

https://www.rapidtables.com/convert/number/index.html
give the result (2):

1100000011010001111010000100100000000000000010101100000000000000000000000000000000000000000000

an small python script delivers the same as 1 (different from 2 and node red) ....

i hope i so not make an error ....

Ah yes, of course.

The largest number you can represent in a JavaScript Number type is 0x1fffffffffffff

So the parseInt() call will be returning a truncated value.

this difference come up as i run the python script and the node red ... my problem is i need binary values between position 39 and 58 anything before and after is not relevant ... (just as explanation the hex value is the hex id of an RFID tag and between position 39 and 58 the item is that i need :slight_smile: )

It's worse than that - it looks like it's returning incorrect bits from hex digit 12 onwards. Note caveman's results (1) and (2) above.

ok ... not so good. ... i could call the python script ... but from inside an function node ...))

Since hex values are equivalent to 4 binary digits each, it might be easier to extract characters 9 to 15 and just convert those to binary. You won't run into any size limitations as it's only 20 bits.

this is probably the best way ...

i think i have it ...

// cutout only position 8 to 16
msg.payload = msg.payload.slice(7,16);
// define as hex
msg.payload = parseInt("0x"+msg.payload).toString(2);
// cutout only 1 to 12
msg.payload = msg.payload.slice(10,28);
// convert to dec
msg.payload = parseInt(msg.payload,2);


return msg; 

i tested with 2 hex values and get the expected result.

Thanks to all for support !!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.