# ASCII TO HEX CONVERSION

**URL:** <https://forums.digi.com/t/ascii-to-hex-conversion/9878>\
**Category:** Rabbit\
**Created:** [May 3, 2007, 5:03pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878 "2007-05-03T17:03:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsalas](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@wsalas](https://forums.digi.com/u/wsalas)\
**Post date:** [May 3, 2007, 5:03pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878/1 "2007-05-03T17:03:07Z")

</div>

Can anyone suggert me how to convert a asccii character to a hexadecimal value (example: 38 asccii to 08 hex)

Thank

Wilmer

---

<div class="post-metadata">

**Author:** ![seulater](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@seulater](https://forums.digi.com/u/seulater)\
**Post date:** [May 3, 2007, 6:14pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878/2 "2007-05-03T18:14:03Z")

</div>

if you are using the result in a math function look up the Functions atof, atoi & atol.

If you are just going to be printing the result using printf then use %x

printf("%x  
",38);

---

<div class="post-metadata">

**Author:** ![bsprouse](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bsprouse](https://forums.digi.com/u/bsprouse)\
**Post date:** [May 3, 2007, 7:42pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878/3 "2007-05-03T19:42:53Z")

</div>

The functions atof, atoi & atol will not help you convert to hex. Also, these are string based functions that expect a null terminated string, not just a single character value. If you are actually looking to convert a string of hex digits into a value, then there is a string function that does this. The strtol function has a base parameter, set that to 16 and you get hex conversions of a string.

If what you want is to convert just a single character value, then this can be done by the following:

char ascii, hex\_value;

ascii = ‘8’;

ascii = toupper(ascii);  
if (isxdigit(ascii)) hex\_value = ascii - (ascii \> 0x39 ? 0x37 : 0x30);

This basic formula assumes that ascii contains a valid hex digit (in ascii, of course) and that all hex digits A thru F are given in upper case. If these conditions are guaranteed, then the toupper call and if (isxdigit(ascii)) are not necessary.

---

<div class="post-metadata">

**Author:** ![wsalas](https://avatars.discourse-cdn.com/v4/letter/w/45deac/32.png) [@wsalas](https://forums.digi.com/u/wsalas)\
**Post date:** [May 3, 2007, 7:47pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878/4 "2007-05-03T19:47:37Z")

</div>

Seulater,

Thank by your help, I heve a string of asccii character and want to get out it by a port of 8 bits, I must to take 2 characters toguether to get out by the port (example: 31,39 ASCII , like 19 hex)

Wilmer

---

<div class="post-metadata">

**Author:** ![seulater](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@seulater](https://forums.digi.com/u/seulater)\
**Post date:** [May 3, 2007, 7:58pm UTC](https://forums.digi.com/t/ascii-to-hex-conversion/9878/5 "2007-05-03T19:58:53Z")

</div>

> [@bsprouse;388](#):
>
> The functions atof, atoi & atol will not help you convert to hex. .

Opps! i meant to type strtol, sorry guys…:o
