# Hex to ASCII for display

#### mukesh1

Joined Mar 22, 2020
68
Lets say we have HEX value,

(byte & 0xF0) // send upper nibble
((byte << 4) & 0xF0); // send lower nibble

How do I convert HEX to ASCII and display on LCD without sprintf itoa ?

#### Dave Lowther

Joined Sep 8, 2016
224
Lets say we have HEX value,

(byte & 0xF0) // send upper nibble
((byte << 4) & 0xF0); // send lower nibble

How do I convert HEX to ASCII and display on LCD without sprintf itoa ?
If your byte contains, for example, 0x41 which is the ASCII code for 'A' what do you want to show on the LCD "A" or "41"?

#### mukesh1

Joined Mar 22, 2020
68
If your byte contains, for example, 0x41 which is the ASCII code for 'A' what do you want to show on the LCD "A" or "41"?
I want to show " 41" on LCD display

#### Dave Lowther

Joined Sep 8, 2016
224
I want to show " 41" on LCD display
For each nibble:
if nibble < 10 then output char or byte value of '0' + nibble
else output char or byte value of 'A' + nibble - 10

#### click_here

Joined Sep 22, 2020
548
You have 2 numbers...
Code:
lsb = number & 0x0F;
msb = (number & 0xF0) >> 4;
If the l/msb is below 10, add '0'
Code:
lsbAsASCII = lsb + '0';
If l/msb is 10 or over...
Code:
lsbAsASCII = (lsb - 0x0A) + 'A';

Last edited:

#### Dave Lowther

Joined Sep 8, 2016
224
You have 2 numbers...
I think the OP just has one number contained in a byte
So where you said "msb = (number & 0xF0) >> 8;" I think the 8 should be a 4
Edit - ignore this as the post I was talking about has been edited to correct it

Last edited:

#### MrSalts

Joined Apr 2, 2020
2,767
What kind of LCD do you have? A 1602 (2x16 character display) or a pixelated graphic display (126x64 or other)?

#### click_here

Joined Sep 22, 2020
548
I think the OP just has one number contained in a byte
So where you said "msb = (number & 0xF0) >> 8;" I think the 8 should be a 4
Fast fingers - I quickly edited it, but you managed to catch me!

#### Dave Lowther

Joined Sep 8, 2016
224
Fast fingers - I quickly edited it, but you managed to catch me!
Sorry. I've edited my post now to say it should be ignored as you corrected your post.

#### MrChips

Joined Oct 2, 2009
30,464
C:
lsb = (byte & 0x0F);
msb = (byte & 0xF0) >> 4;
lsb = (lsb < 10) ? lsb : lsb + 7;
msb = (msb < 10) ? msb : msb + 7;
lsb += '0';
msb += '0';

#### ApacheKid

Joined Jan 12, 2015
1,452
If your byte contains, for example, 0x41 which is the ASCII code for 'A' what do you want to show on the LCD "A" or "41"?
There is an alternative approach, rather than executing code for this create a lookup table, this will execute pretty quickly.

input of 0 -> '00'

input of 117 -> '75'

input pf 255 -> 'FF'

Of course if you cant spare 512 bytes then this won't be possible but if memory is not too constrained this is a neat option to consider. With some additional simple code you can just have a table of 16 bytes not 255, but that needs a bit of division.