Hex to ASCII for display

Thread Starter

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"?
 

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:

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.
 
Top