From Hex to Text

Do you speak hex? As a programmer, do you look at 0xF and see 1111 in binary? Do you see 15 decimal? What about the ASCII code? Do you know the letter for code 0x41? Are you that good?

The character for ASCII code 0x41 is 'A', which you’d know if you are either a total nerd or have been programming for some time — or both! I never set out to memorize hex / decimal / binary / ASCII; it just happened.

Still, occasionally the nerds present information in a fashion designed to be easily picked up by nerds, but ignored by anyone else.

Recently I saw a post about computer pioneer Dennis Ritchie. He died the same year as Steve Jobs, but few people lavish the praise upon Dr. Ritchie that they attribute to Jobs. In fact, Jobs contributions to computer science pale in comparison to what Dr. Ritchie did, including inventing the C language and Unix.

I’ll leave it up to you to review Dr. Ritchie’s many accomplishments. Yet, recently I saw someone compare Ritchie and Jobs, at the end of which they wrote:

79 6f 75 20 74 77 61 74 73

The numbers I recognized as hexadecimal, thanks to the F in 6f. I’ve not memorized the ASCII table completely, but I know that the codes represent characters — text. The 0x020 specifically is the space. To translate the rest of the codes into characters, I’d either have to examine an ASCII table or write code that translates the ASCII into text.

I did the latter.

Here you find the code I wrote to translate hex bytes, similar to those formatted above, and generate the corresponding ASCII string. The code doesn’t check input, so it’s not bulletproof. It’s something I wrote quickly just in case the issue came up again:

#include <stdio.h>

#define SIZE 32

int main()
{
    char output[SIZE];
    int c;
    int x = 0;

    printf("Type hex (l/c) digits, 0 to end: ");
    while(x < SIZE)
    {
        scanf("%2x",&c);
        output[x] = (char)c;
        if(c == 0) break;
        x++;
    }
    printf("\n%s\n",output);

    return(0);
}

Line 7 creates a buffer for 32 characters, which is 31 characters plus the '\0' at the end of a string.

The while loop at Line 12 uses variable x as an index to count characters. Text is read at Line 14 with the scanf() function, which is set to recognize 2-digit hexadecimal values as input.

The character is stored in the output[] buffer at offset x in Line 15.

Line 16 tests for value 0 as input, which terminates the loop before x reaches 32.

Line 19 prints the result.

Here is a sample run:

Type hex (l/c) digits, 0 to end: 48 65 6c 6c 6f 21 0

Hello!

The scanf() function terminates input at any whitespace character, so I could type the hex digits each on a line by itself or just use spaces to separate each.

Because I found this utility useful, I saved in my personal bin folder. I named it hex2ascii. Naturally, being a nerd, I felt the need to write a companion program ascii2hex, which works the other way: It translates a short text string into a series of hex bytes. I’ll show you that code in next week’s Lesson.

One thought on “From Hex to Text

  1. The original version of this post’s code contained an error. Lines 15 and 16 were swapped, so the while loop exited before code 0 was written to the string. The effect was that the string was improperly terminated. I didn’t catch the error on my compiler, but reader Jacco caught it. Thanks!

Leave a Reply