# Thread: How to convert byte into ASCII

1. ## How to convert byte into ASCII

Hello

I am working on the project where I need to send ASCII numbers to to LCD

any idea how to do convert it in main function

Code:
```void LCD (unsigned char *PTR )
{
//LCD code
}

main ()

{
send (0x31);
send (0x45);
send (0x61);

... so on
}``` 2. Convert the "number" to a string and then "send" each character of that string. 3. Originally Posted by jimblumberg Convert the "number" to a string and then "send" each character of that string.

41 = 4×16^1+1×16^0 = 64+1 = 65 = 'A' character
30 = 3×16^1+0×16^0 = 48 = '0' character

How to convert this technique in c code 4. Why not just use sprintf() to convert the "number" to a string? By number I mean something like 100983.

Do you not understand that 41 (hex) already equals 65 (decimal) which is equal to the 'A' character? 5. "ASCII numbers" are just numbers. The only time the "ASCII" part matters is when you're interpreting that number as a character.

In C, a char or unsigned char value is just a number. What makes it a "character" is that putting a source code character in (') apostrophes for a char constant or inside (") quotes as part of a string constant will convert that character to the corresponding number. The number is what the C program deals with. In particular, the constant 'B' is an int constant in C, not a char. Assuming that ASCII is the compiler's character set, there is absolutely no difference between using 'B', 0x42, or 66 in a C program. They are all different ways of writing the same number.

If you want to display that value as a character, you usually send it to a device unchanged, as in send(66) or send('B'). If you want to draw those characters on an LCD panel that doesn't support it, that needs a chapter in a book rather than a Q&A answer.

If you want to send a numeric representation, convert the number to a string and send the characters one at a time. Here's one way to do that to "send" as character as two hexadecimal digits:
Code:
```void send_hex(int c)
{
const static char hextab[] = "0123456789ABCDEF";
send(hextab[c & 0xF]);
send(hextab[(c >> r) & 0xF]);
}```
You can also use sprintf() to convert a number to a string in decimal, hexadecimal or octal format. That's usually best avoided in low-level code. (I assume that any code directly interfacing to an LCD device is low-level.) You usually want the smallest reasonable code size, and sprintf() is *not* small. Most C compilers implement _itoa(), but if you know you need precisely 3 decimal digits, it's probably best to just write the C code, something like:
Code:
```    send('0' + c/100%10);
send('0' + c/10%10);
send('0' + c%10);``` Popular pages Recent additions ascii, convert, lcd, main, send 