Thread: How to convert byte into ASCII

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    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. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Convert the "number" to a string and then "send" each character of that string.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by jimblumberg View Post
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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?

    Perhaps you should study the ASCII chart for more information.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    12
    "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 subscribe to a feed

Similar Threads

  1. Convert hex byte array to hex char*
    By C progammer in forum C Programming
    Replies: 6
    Last Post: 11-02-2018, 10:33 AM
  2. 7bit ASCII out of 4 byte value
    By ghostcoder in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2010, 09:18 AM
  3. Replies: 6
    Last Post: 04-18-2010, 08:12 PM
  4. ANSI C ASCII String to HEX byte array conversion
    By phyte in forum C Programming
    Replies: 10
    Last Post: 12-14-2004, 08:02 AM

Tags for this Thread