# Convert hex in bin

**URL:** https://discourse.nodered.org/t/convert-hex-in-bin/28886
**Category:** General
**Created:** [24 June 2020 13:02 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886 "2020-06-24T13:02:23Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 13:02 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/1 "2020-06-24T13:02:23Z")

</div>

Hi all,

i use in an Node Red function node:

```auto
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 🙂

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 June 2020 13:23 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/2 "2020-06-24T13:23:15Z")

</div>

When I convert `0x30347A120002AF4400000001` to binary, I get:

```auto
> 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](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?

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [24 June 2020 13:47 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/3 "2020-06-24T13:47:37Z")

</div>

> [@knolleary](#):
>
> When I convert `0x30347A120002AF4400000001` to binary, I get:
> 
> ```auto
> > parseInt("0x30347A120002AF4400000001").toString(2)
> '1100000011010001111010000100100000000000000010101100000000000000000000000000000000000000000000'
> 
> ```

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.

---

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 13:51 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/4 "2020-06-24T13:51:48Z")

</div>

Running in Node Red i get:

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

```

i am confused ...

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

```auto
1100000011010001111010000100100000000000000010101011110100010000000000000000000000000000000001

```

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

```auto
1100000011010001111010000100100000000000000010101100000000000000000000000000000000000000000000

```

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

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 June 2020 13:54 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/5 "2020-06-24T13:54:43Z")

</div>

> [@molesworth](#):
>
> My first guess would be a lack of sufficient digits to handle 96 bit values.

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.

---

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 13:57 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/6 "2020-06-24T13:57:33Z")

</div>

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 🙂 )

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [24 June 2020 14:00 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/7 "2020-06-24T14:00:19Z")

</div>

> [@knolleary](#):
>
> So the `parseInt()` call will be returning a truncated value.

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.

---

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 14:01 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/8 "2020-06-24T14:01:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [24 June 2020 14:05 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/9 "2020-06-24T14:05:58Z")

</div>

> [@caveman](#):
>
> my problem is i need binary values between position 39 and 58 anything before and after is not relevant

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.

---

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 14:11 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/10 "2020-06-24T14:11:11Z")

</div>

this is probably the best way ...

---

<div class="post-metadata">

### Author: ![caveman](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@caveman](https://discourse.nodered.org/u/caveman)
#### Post date: [24 June 2020 15:02 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/11 "2020-06-24T15:02:53Z")

</div>

i think i have it ...

```auto
// 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 !!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [8 July 2020 15:02 UTC](https://discourse.nodered.org/t/convert-hex-in-bin/28886/12 "2020-07-08T15:02:53Z")

</div>

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