How to convert an ASCII char to a 'binary' string representation in C

A simple tutorial using ANSI C

Converting a char to a string of zeroes and ones – that is, creating a string that is a binary representation of an unsigned integer with a maximum value of 255– is the kind of problem that can be a pain if you're starting out in programming. And it is also the sort of problem loved by university lecturers.

It's an easy process in most programming languages – in fact, a number will have standard functions that let you do it in a line or two. But with a slightly lower level language like C it can be a pain, especially if you have to do it the 'hard' way and only use the standard (ANSI) library.

The process relies on division by 2 (i.e. the base of binary numbers, just as 10 is the base for the numbers we're used to dealing with). The solution below is based on having a maximum value of 255 for the input - just enough for a char. The idea is to start at the most significant 'bit' in the output binary string, and work towards the least significant 'bit'.

Parameters:
input: The char you want to convert to a string of binary digits.
*output: Pointer to an array of char. It will need to be big enough to hold the output string.

/*We're going to convert a char to a string of 'binary' digits*/
/*That is, base 2 */

#define BASE 2;

/*We know that the maximum number of characters in the*/ /*string will be 8 to provide a string representation */ /*of a byte */

#define DIGITS 8;

void convertBaseVersionOne(char input, char *output) { int i, remainder;

char digitsArray[3] = "01"; /*Why this array? Because we can very easily change this for*/ /*convertBaseVersionTwo(), where we will convert to a */ /*'hexadecimal string' */

/*And here is where the very straightforward 'magic' happens: */ /* Going backwards through the array of eight characters, */ /* starting at the most significant 'bit' i.e. the leftmost one . */

for (i = DIGITS; i > 0; i--) { remainder = input % BASE; input = input / BASE; output[i - 1] = digitsArray[remainder]; }

/*We're creating a string, so we need to have the null character on the end:*/ output[digits] = '\0';

}

So what about hexadecimal?

To convert to a base 16 (hex) string representing the char, we just need to update the base, the number of digits the final string will have (you only need two to represent a char).

#define BASE 16;
#define DIGITS 2;

void convertBaseVersionOne(char input, char *output) { int i, remainder; /*(With hex, you need the rest of these characters, not just 0 and 1)*/ char digitsArray[17] = "0123456789ABCDEF";

for (i = DIGITS; i > 0; i--) { remainder = input % BASE; input = input / BASE; output[i - 1] = digitsArray[remainder]; } output[digits] = '\0'; }

A more general purpose version

This version will let you convert binary, octal or hex numbers, as long as you feed it the appropriate parameter. So you can do a ternary conversion, or, possibly more usefully, an octal conversion.

void convertBaseVersion(char input, int base, char *output, int digits) { int i, remainder; char digitsArray[17] = "0123456789ABCDEF";

for (i = digits; i > 0; i--) { remainder = input % base; input = input / base; output[i - 1] = digitsArray[remainder]; } output[digits] = '\0'; }

Join the newsletter!

Error: Please check your email address.
Show Comments
[]