Conversion from Byte to ASCII in C

46,864

Solution 1

First of all you should take some more care on the formulation of your questions. It is hard to say what you really want to hear. I think you have some binary blob and want it in a human readable form, e.g. to dump it on the screen for debugging. (I know I'm probably misinterpreting you here).

You can use snprintf(buf, sizeof(buf), "%.2x", byte_array[i]) for example to convert a single byte in to the hexadecimal ASCII representation. Here is a function to dump a whole memory region on the screen:


void
hexdump(const void *data, int size)
{
    const unsigned char *byte = data;

    while (size > 0)
    {
        size--;
        printf("%.2x ", *byte);
        byte++;
    }
}

Solution 2

Well, if you interpret an integer as a char in C, you'll get that ASCII character, as long as it's in range.

int i = 97;
char c = i;

printf("The character of %d is %c\n", i, c);

Prints:

The character of 97 is a

Note that no error checking is done - I assume 0 <= i < 128 (ASCII range).

Otherwise, an array of byte values can be directly interpreted as an ASCII string:

char bytes[] = {97, 98, 99, 100, 101, 0};

printf("The string: %s\n", bytes);

Prints:

The string: abcde

Note the last byte: 0, it's required to terminate the string properly. You can use bytes as any other C string, copy from it, append it to other strings, traverse it, print it, etc.

Share:
46,864
name_masked
Author by

name_masked

Primary programming languages: Java, C Inclined on learning: Python, Android app Development

Updated on July 18, 2022

Comments

  • name_masked
    name_masked almost 2 years

    Can anyone suggest means of converting a byte array to ASCII in C? Or converting byte array to hex and then to ASCII?

    [04/02][Edited]: To rephrase it, I wish to convert bytes to hex and store the converted hex values in a data structure. How should go about it?

    Regards, darkie

  • Felix Kling
    Felix Kling about 14 years
    Do you want to say that characters are integers?
  • Felix Kling
    Felix Kling about 14 years
    The integer does not have to be 0 <= i < 128 or (32 <= i < 128 for printable chars). If you convert an integer to a character, the integer is truncated to the 8 LSBs (least significant bits). Only those have to be in the range. E.g. i = 1121 gives the same result as i = 97 (1121 = 1024 + 97)
  • Eli Bendersky
    Eli Bendersky about 14 years
    @Felix: true, but then it doesn't make much sense since you get the modulo 256 of the int, which is unlikely to be the desire of the programmer.
  • Alok Singhal
    Alok Singhal about 14 years
    Nitpick: 'a' may or may not be 97 in C. On most computers today, it is, but the C standard doesn't guarantee this. To do this portably, one has to make a lookup table. Of course, 'a' == 97 is true on most of the systems today.
  • zellio
    zellio about 14 years
    Actually no I don't because that is wrong, they are not. They are generally of different size (byte versus word) however you can treat characters as if they were integers and vice versa.
  • name_masked
    name_masked about 14 years
    Hi, I meant how I can convert bytes to ASCII and store those values, maybe in an array?
  • Eli Bendersky
    Eli Bendersky about 14 years
    @darkie15: I don't understand what you mean by that. Care to elaborate (or maybe update the question with an example)??
  • name_masked
    name_masked about 14 years
    Okie ... Now hex form can be achieved by using %x format. But this is only for printing .. I want to store that hex form, say in a array .. so i can use it later for more processing.
  • Eli Bendersky
    Eli Bendersky about 14 years
    @darkie15: how about sprintf (or snprintf then?