Hex Parsing – Solution

Two solutions are possible for this month’s Exercise, a string solution and a value solution. One is good for parsing the hex string right-to-left, the other for parsing the string left-to-right.

Common to both solutions is the random number generation; where the solutions differ is how the number is stored.

For the string solution, the number is stored as a string in a buffer. The sprintf() function handles that task:

    /* Generate random number */
    srand( (unsigned)time(NULL) );
    r = rand();

    /* convert value to string */
    sprintf(buffer,"%X",r);
    printf("Original value: %s\n",buffer);

Once the value is stored as a string (in buffer, above), the code needs to locate the final character in the hexadecimal number. That’s necessary because the random value could be any length up to 8 characters (depending on the compiler). Once found, the string is displayed backwards (right-to-left) one character at a time. Here’s the code I used to perform that task, where buffer is the string’s location and b is a pointer variable:

    /* find last character */
    b = buffer;
    while(*b++);    /* set b->'\0' +1 */
    b-=2;           /* back up to last char */
    while(b >= buffer)
        printf("%c\n",*b--);

Click here to view the entire code.

The other solution, the one you probably didn’t try, is to work with the random value as a value. The secret to making the solution work is to shift the value’s bits to the right and chop off one hexadecimal value (byte) at a time. Here is that crazy solution:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r,digits,byte;

    /* Generate random number */
    srand( (unsigned)time(NULL) );
    r = rand();

    printf("Original value: %X\n",r);

    digits = 8;
    while(digits)
    {
        byte = r & 0xF;
        printf("%X\n",byte);
        r = r >> 4;
        if( r == 0 )
            break;
    }

    return(0);
}

This solution is really nerdy. As an ancient Assembly language programmer, this code demonstrates my primitive level of thinking regarding binary data.

Variable digits (Line 15) is set to 8, which is the maximum number of digits in an int value for most compilers. The while loop spins through those digits, starting at Line 16.

At Line 18, an AND mask peels off all but the last byte of the random value. The printf() statement in Line 19 uses %X placeholder to display that byte. Because it’s the last digit in the value, this statement satisfies the Exercise’s solution.

To display the next byte, the entire value is shifted to the right by 4 bits in Line 20. If the value is equal to zero (Line 21), then the last digit was displayed and the loop can terminate. Otherwise, the loop repeats, displaying the far right hex value.

If you want to visually see how the process works, change the printf() statement at Line 19 to read:

        printf("%8X - %X\n",r,byte);

Here’s sample output:

Original value: 330CAC77
330CAC77 - 7
 330CAC7 - 7
  330CAC - C
   330CA - A
    330C - C
     330 - 0
      33 - 3
       3 - 3

Above, you see the value shifted right 4 bits. Each time the AND mask helps lop off the final value.

You may have arrived at your own, clever solution. If so, great! As long as the output shows each digit from the hex value, right-to-left, you completed the hex parsing Exercise.

Leave a Reply