Hex Parsing

Parsing is the process of sifting through a chunk of data to look for significant information. It’s a big topic.

In the course of your programming career (or hobby), you’ll find yourself frequently challenged with a parsing puzzle. A common one is to examine command line options, which I’ve covered in a series of blog posts earlier this year.

At a higher level, computer data is frequently — and hopefully — organized in some type of sane and rational manner. The goal is to help the programmer (you) quickly locate relevant information without having to brute force a search.

The good news is that many popular schemes exist for organizing data. These schemes include JSON, XML, and whatever else is trendy. The better news is that C language libraries are available to help you access specific tidbits of information in commonly-formatted data. That way you don’t have to write your own search routines. You’re not so fortunate with this month’s exercise.

Your challenge this month is a pretty basic form of parsing: Slice a random hexadecimal value into its individual hex digits. Here’s an example:

Original value: 6B0B1DB2
2
B
D
1
B
0
B
6

The value 6B0B1DB2 is random. The code parses that value, slicing off each hex digit one after the other and displaying the digit on a line by itself.

Just to be cruel, as well as to prove that multiple solutions exist, the first digit sliced off must be the final digit of the random value generated, as shown above with the digit 2.

Please try this exercise before checking out my solutions.

Leave a Reply