# HEX\>DEC Convert in XOD?

**URL:** <https://forum.xod.io/t/hex-dec-convert-in-xod/690>\
**Category:** Uncategorized\
**Created:** [April 19, 2018, 4:15pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690 "2018-04-19T16:15:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![triko](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@triko](https://forum.xod.io/u/triko)\
**Post date:** [April 19, 2018, 4:15pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/1 "2018-04-19T16:15:23Z")

</div>

Hey everyone im here to find if there is a way to accomplish a conversion from hex\>dec in XOD, because a LCD library need the LCD addr and without convert it to decimal by yourself i want that user are able to upload directly the hex number or dec number!  
More dynamic

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [April 19, 2018, 4:18pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/2 "2018-04-19T16:18:54Z")

</div>

The best option for now is just to use a calculator.

For example, [https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=0x37](https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=0x37)

We’ll make custom literals with support for hex and bin a bit latter.

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [April 19, 2018, 5:21pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/3 "2018-04-19T17:21:07Z")

</div>

I was trying to create a node that does the hex-\>dec conversion. The sscanf function makes it pretty trivial:

struct State {  
};

{{ GENERATED\_CODE }}

void evaluate(Context ctx) {  
auto hex = getValue\<input\_HEX\>(ctx);  
int dec;  
sscanf(“0x3F”, “0x%x”, &dec);  
emitValue\<output\_DEC\>(ctx, dec);  
}

But when I replace “0x3F” with hex, it gives a data mis-match. How do I convert XOD string (hex variable) to char\* for sscanf?

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [April 20, 2018, 8:21am UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/4 "2018-04-20T08:21:58Z")

</div>

XOD strings are tricky multi-fragment memory views. There’s a `dump` function to convert them into plain C-string:

```cpp
XString hex = getValue<input_HEX>(ctx);
char hexBuff[16] = { 0 }; // unsafe 16 char cap, init with terminal zeros
dump(hex, hexBuff);
// sscanf(hexBuff, ...);

```

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [April 20, 2018, 3:17pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/5 "2018-04-20T15:17:20Z")

</div>

> [@nkrkv](#):
>
> char hexBuff[16] = { 0 }; // unsafe 16 char cap, init with terminal zeros  
> dump(hex, hexBuff);

That did the trick. gweimer/utils now has a hex-to-number node for converting a hex string into a number. It expects a leading “0x” as part of the string. Your program will blow up if your hex string is \>16 characters long due to a buffer over-flow, but I can’t imagine anyone legitimately using more than 6-8 characters. If I can figure out how to get the length of XString hex, I can remove that bug.

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [April 20, 2018, 4:24pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/6 "2018-04-20T16:24:03Z")

</div>

> [@gweimer](#):
>
> If I can figure out how to get the length of XString hex, I can remove that bug.

```cpp
XString hex = getValue<input_HEX>(ctx);
size_t len = length(hex);

```

The funcs are documented in [C++ API reference](https://xod.io/docs/reference/node-cpp-api/#list-functions).

---

<div class="post-metadata">

**Author:** ![triko](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@triko](https://forum.xod.io/u/triko)\
**Post date:** [April 20, 2018, 6:00pm UTC](https://forum.xod.io/t/hex-dec-convert-in-xod/690/7 "2018-04-20T18:00:18Z")

</div>

Magic I will download that and implement in my LCD library 😃 Thank you boss
