Converting hex string to binary in c


All feedback is welcomed: I spent some time thinking about different things that I would have to take into consideration when doing the conversion of the string to the byte array. We live in a World where we can't trust any, not even our input string.

So, I need to have some type of validation in place for the input. Once I have determined that I actually have some type of string. I would handle rest of conditions during the conversion, except the 'dropped' leading zero '0' which I would handle before the actual conversion. For the actual conversion I decided to take a forked solution: The upper and lower nibble, which I would the convert individual to the equivalent byte value. This would be the time to validate that the received character is actually an alphanumeric character.

Now to the fun part. I would break my solution into 2 distinct functions: This function would be the public function exposed to the caller.

It would be responsible for doing basic input validation, handle the items 1, 2 and 3 from the special cases that I had identified as well as constructing the byte array to be returned to the caller. This function would be the private work horse function. The main responsiblity would be the convert the character in hand to the equivalent byte value. In case of non-numeric value, this function would handle the casing and validation of the character.

Now, to reuse this code the function would have to take into account the nibble order and shift the value accordingly. Now, I understand that this function take a simple string as input.

Support for stream is easy to add. The calling this function is very simple. I created a class named 'StringConverter' to hold them. Thank you for posting it — I had the exact same problem as you where I had a string in the format "0x98efdg53" and needed to convert it to a byte array in C. All the build in methods kept giving me overflow errors etc, and this method works great! In my situation, I had a text box that the user can enter a number value. The string would come out like this but for program, I needed to revers it like 01 02 03 04 and load it in a byte array like so 0x01, 0x02, 0x03, 0x I was able to just copy and paste your code and it worked beautifully.

The Problem Convert a string it to a byte array representing the actual text values of each byte. Considerations I spent some time thinking about different things that I would have to take into consideration when doing the conversion of the string to the byte array.

Input is valid, and expected but it creates special handling conditions. Is the input string null or empty The string starts with the HEX indicator string '0x'.

Is the leading '0' dropped from the string. The casing of the letter, they can be upper, lower or mixed casing. Does the string contain characters that are not valid alphanumeric values: Approach Taking into account the factors that I came up with, I came up with the following approach.

My Solution Now to the fun part. Adjust starting index accordingly. Usage The calling this function is very simple. I was actually suprised with the performance results that I received. Conclussion The results are suprising, and not too bad for about an hours worth of work. October 7, at 7: July 26, at 4: July 31, at 8: In addition if the post caught your interest and you love C programming in general why not check my work in the Refu Library?

It currently needs more contributors and people who are able and willing to provide feedback! Hey Michael, Thanks for the comment.

This is rather an old post and I moved it from another host so the html characters appeared out of the blue. Your email address will not be published. Notify me of follow-up comments by email. Notify me of new posts by email. Skip to content This post presents a nice and efficient algorithm to convert an integer into a hexadecimal string using the properties of binary numbers.

Binary Representation of a Decimal Hexadecimals are numbers with 16 as a base, which is a power of 2. Binary Number divided into groups of 4 bits It looks much more presentable now, right?

Hexadecimal conversion Putting all of the above into C code is very easy.