Do you learn better from video?
Learn faster with deeper understanding! |
Input the binary value | The hex result is | |
---|---|---|
Spaces |
Spaces |
Converting from binary to hex is a straightforward procedure. Just remember to do the necessary padding, before making the substitution. Here is how to do the calculations:
Here is a table that contains the binary to hex mappings:
Download the complete example from here: bin-to-hex.zip
While converting from binary to hex is very easy on paper, it could get a bit tricky to code. The key moments here are the padding and do the length calculations correctly. The it is just string compare and copy.
Let's start with the input. We are converting from one string type to another string. This means that we are not limited by 32-64 bit numbers. We can convert as big numbers, as we like. However, choosing a round size(like 50 or 100) for the binary string is not a good idea, because the data usually comes in complete bytes. For that reason, it is better to choose a size that is multiple of 8, like 64, 96, 128.
char
*hex;
char
binary[97]; //96 digits + 1 terminating symbol
scanf("%96s",
binary); //Don't read beyond 96 symbols to prevent overflow
Once we read the input, it is time to begin the algorithm.
Note, that here we want to add zeros to a complete byte. This is, because it will be better for the output formatting of the hex value.
Padding with starting zeros is an easy task, if you apply a little smart arithmetic to calculate the necessary padding. Its length is determined only by the size of the left-most incomplete group(here we group by 8). If the group is complete, then we don't need to pad anything. If the group is of 3 digits, we need to add 5 0s before its start. To determine that size we use the formula:
padding = 8 - length % 8;
Here length % 8 gives the number of digits in the left most group.
Once we know the padding size we allocate memory for the padded binary string. Then copy the zeros and the binary value. Finally we reset the pointer to the beginning and return the result.
char
*padBinary(char
*binary, int
length)
{
int
i;
int
padding = 8 - length % 8;
int
paddedBinaryLength = padding + length + 1;
char
*paddedBinary;
if(padding
== 8 || padding == 0)
return
binary;
paddedBinary
= (char
*)malloc(paddedBinaryLength);
if(paddedBinary
== NULL)
exit(1);
for(i
= 0; i < padding; ++i)
*paddedBinary++
= '0';
while(*binary
!= '\0')
*paddedBinary++
= *binary++;
*paddedBinary
= '\0';
paddedBinary
-= paddedBinaryLength - 1;
return
paddedBinary;
}
Now we can convert that binary to hex. We determine the number of digits for the hexadecimal number with the formula:
hexLength
= strlen(paddedBinary) / 4; //We
need one hex symbol for every 4 binary symbols
hexLength
= hexLength + hexLength/2 + 1; //Make
place for a space after every two symbols + the null terminator
I think, the comments explain it well enough.
The next step is to allocate the memory and convert every group of 4 to its hex representation. That we do with the function valueOf, according to the table above.
Finally, we reset the hex pointer and print the result.
char
*binaryToHex(char
*binary, int
length)
{
int
i;
char
*hex;
char
*paddedBinary = padBinary(binary, length);
int
hexLength = strlen(paddedBinary) / 4; // We
need one hex symbol for every 4 binary symbols
hexLength
= hexLength + hexLength/2 + 1; // Make
place for a space after every two symbols + the null terminater
hex
= (char
*)malloc(hexLength);
if(hex
== NULL)
exit(1);
for(i
= 0; i < length; i += 8)
{
char
halfByte[5];
//
copy the 4 binary digits and decode them to one hex digit
memcpy(halfByte,
paddedBinary, 4);
halfByte[4]
= '\0';
*hex++
= valueOf(halfByte);
paddedBinary
+= 4;
memcpy(halfByte,
paddedBinary, 4);
halfByte[4]
= '\0';
*hex++
= valueOf(halfByte);
paddedBinary
+= 4;
*hex++
= '
';
}
*hex
= '\0';
hex
-= hexLength - 1;
return
hex;
}
If you liked this article, you also might want to look at:
Do you learn better from video?
Learn faster with deeper understanding! |
Did this help? Support me with your vote ;-) |
|
|
Did this help? |
|
|