# Convert hexdecimal to binary

**URL:** <https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540>\
**Category:** General\
**Created:** [12 January 2025 07:38 UTC](https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540 "2025-01-12T07:38:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [12 January 2025 07:38 UTC](https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540/1 "2025-01-12T07:38:06Z")

</div>

Hi. What am I doing wrong?  
Where am I wrong?

```auto
'use strict';

var convertBase = function () {
    function convertBase(baseFrom, baseTo) {
        return function (num) {
            return parseInt(num, baseFrom).toString(baseTo);
        };
    }
    // hexadecimal to binary
    convertBase.hex2bin = convertBase(16, 2);
    return convertBase;
}();

var newMsg = convertBase.hex2bin('ff'); // '11111000'
return newMsg;

```

Writes an error "Function tried to send a message of type string"  
They need a string. How to do it?  
Thank you.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 January 2025 10:06 UTC](https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540/2 "2025-01-12T10:06:42Z")

</div>

> [@Le-Ze](#):
>
> error "Function tried to send a message of type string

A quick search in the forum for "Function tried to send a message of type " would have saved you here.

In short, A function node must always return either the original message or a new object. The preference being returning the original msg

```auto
// Your code here
msg.payload = call_your_function(x)
return msg

```

Ps, did you use an AI for that function? It's a bit over the top! Something like below is all this really needs

```auto
const hex = "FF" // or msg.payload
msg.payload = (parseInt(hex, 16).toString(2)).padStart(8, '0')
return msg

```

---

<div class="post-metadata">

**Author:** ![Le-Ze](https://avatars.discourse-cdn.com/v4/letter/l/a87d85/32.png) [@Le-Ze](https://discourse.nodered.org/u/Le-Ze)\
**Post date:** [12 January 2025 10:24 UTC](https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540/3 "2025-01-12T10:24:45Z")

</div>

It works. Thank you.

---

<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:** [26 January 2025 10:24 UTC](https://discourse.nodered.org/t/convert-hexdecimal-to-binary/94540/4 "2025-01-26T10:24:51Z")

</div>

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