# Thread: Printing Hex as Binary

1. ## Printing Hex as Binary

I'm doing a fairly large program that acts somewhat as a compiler. I've gotten to a part where I cannot seem to find out how to take the two character object code indexes (in hex) and print it out in binary form. Anyone have any ideas to what functions handle this?

Some of the code:
Code:
`byte opcode[4];  /* each index will contain a two digit hex code */`
The indexes will be each "Objcode in Hex" and output is to the right:
Code:
```     Line   Objcode in Hex   Objcode in Binary
1
2
3     B4 10           10110100 00010000
4     A0 04           10100000 00000100
5     33 20 06        00110011 00100000 00000110
6     57 90 00 00     01010111 10010000 00000000 00000000
7     B4 10           10110100 00010000
8     4F 00 00        01001111 00000000 00000000
9     00 00 00        00000000 00000000 00000000```

2. Hello,

Each hex digit is 4 binary digits 0=0000, 1=0001, 2=0010, 3=0011 ... up to F=1111. Just take each hex digit from left to right and write the binary equivalents from left to right. You can drop any 0s that end up on the left.

Here is a list of Hex and Binary values:
Code:
```Decimal	Hex	Binary
0	0	0000
1 	1 	0001
2 	2 	0010
3 	3 	0011
4 	4 	0100
5 	5 	0101
6 	6 	0110
7 	7 	0111
8 	8 	1000
9 	9 	1001
10 	a 	1010
11 	b 	1011
12 	c 	1100
13 	d 	1101
14 	e 	1110
15 	f 	1111```
With this in mind, the hex number 8cf is 100011001111 in binary.

Edit: Most computers now use "2's complement" arithmetic with negative numbers. One way to solve your problem is to convert Hex 'B' to Decimal '11' then to Binary '1011'.

- Stack Overflow

3. Hello... I know how to convert hex to binary, but what I was asking is should I keep an array that represents what each 15 digits will be in binary and keep calling back to the array? Such as:

Code:
```char codes[16];

/* Decimal   Binary */
codes[0] = 0000
codes[1] = 0001
codes[2] = 0010
codes[3] = 0011
codes[4] = 0100
codes[5] = 0101
codes[6] = 0110
codes[7] = 0111
codes[8] = 1000
codes[9] = 1001
codes[10] = 1010
codes[11] = 1011
codes[12] = 1100
codes[13] = 1101
codes[14] = 1110
codes[15] = 1111```

4. How about something like this?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void foo(size_t line, const char *text)
{
char *end;
printf("%3lu  %-12s  ", (long unsigned)line, text);
do
{
unsigned char bit, result = strtoul(text, &end, 16);
text = end;
for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
{
putchar(result & bit ? '1' : '0');
}
putchar(' ');
} while ( *end != '\0' );
putchar('\n');
}

int main ( void )
{
char *line[] =
{
"B4 10",
"A0 04",
"33 20 06",
"57 90 00 00",
"B4 10",
"4F 00 00",
"00 00 00",
};
size_t i;
for ( i = 0; i < sizeof line / sizeof *line; ++i )
{
foo(i + 1, line[i]);
}
return 0;
}

/* my output
1  B4 10         10110100 00010000
2  A0 04         10100000 00000100
3  33 20 06      00110011 00100000 00000110
4  57 90 00 00   01010111 10010000 00000000 00000000
5  B4 10         10110100 00010000
6  4F 00 00      01001111 00000000 00000000
7  00 00 00      00000000 00000000 00000000
*/```

5. Originally Posted by Mystic_Skies
should I keep an array that represents what each 15 digits will be in binary and keep calling back to the array?
» It is not recommended from my point of view. There is a simple way to convert a decimal to binary. Here is the logic of it:

Code:
```Start a loop that happens 4 or CHAR_BIT times [say i]
If 1 left shift of i is bitwise AND (&) of variable, then print 1
Else print 0```
Q. What is the bitwise AND operator?
A. It is a two-place operator. It returns a word with each bit set only if the corresponding bit is set in both operands.

For example:
Code:
```x = 12;
y = 10;
z = x & y;```
Would result in:
Code:
```	0000 1100
&	0000 1010
---------
0000 1000 = 8 (decimal)```
- Stack Overflow

6. You can do it this way too:
Code:
```itsme@itsme:~/C\$ cat num2.c
#include <stdio.h>
#include <string.h>

int main(void)
{
char *numstr = "37D95B";
char hex[] = "0123456789ABCDEF";
char *bin[] = { "0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111" };

while(*numstr)
{
printf("%s", bin[strchr(hex, *numstr)-hex]);
numstr++;
}
putchar('\n');

return 0;
}
itsme@itsme:~/C\$ ./num2
001101111101100101011011```

7. Thanks everyone for your great replies.