Hexadecimal to Binary Conversion
The number system conversion from hexadecimal number to binary number is explained by algorithm steps and it is tested by C programming implementation.
Hexadecimal number system
The hexadecimal number system has the following unique digits.
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f}
As the hexadecimal number system has the 16 unique digits, it is called base 16 or radix 16 number system.
Each hexadecimal digit can be represented by binary 4 bits, therefore the hexadecimal numbers helps remember long binary sequence.
Binary number system
The Binary number system has the following unique digits.
{0, 1}
As the binary number system has the 2 unique digits, it is called base 2 or radix 2 number system.
The computer can only understand or is operated by binary number system, therefore it accepts data,instruction and everything in binary numbers format.
Hexadecimal to Binary conversion example
The given hexadecima number is A06F and converts it into binary numbers
hexadecimal = A06F , binary = ?
- split each digit of hexadecimal number
- convert each hexadecimal digits into equivalent decimal number
- carried out decimal to 4 bits binary conversion on each decimal number
Hexa = A | 0 | 6 | F Decimal = 10 | 0 | 6 | 15 binary = 1010 | 0000 | 0110 | 1111
if Hexadecimal value = 0xA06F and equivalent binary = 1010000001101111
Hexadecimal to Binary - C program
The number system conversion from hexadecimal number to binary number process implemented by C programming code and tested the results by giving a sample hexadecimal value.
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char* dec2bin(int decval) {
int n=0; int bph=4;
char *bits=(char*) malloc(sizeof(char)*bph+1);
for(int n=0;n<bph;n++)
{
int reminder= decval %2;
decval = decval /2;
bits[bph-n-1] = (reminder==1) ? '1' : '0';
}
bits[bph] = '\0';
return bits;
}
int hex2dec(char hx) {
int decval=0;
switch (hx)
{
case '1' :
decval=1;
break;
case '2':
decval=2;
break;
case '3':
decval=3;
break;
case '4':
decval=4;
break;
case '5':
decval=5;
break;
case '6':
decval=6;
break;
case '7':
decval=7;
break;
case '8':
decval=8;
break;
case '9':
decval=9;
break;
case 'A':
case 'a':
decval=10;
break;
case 'B':
case 'b':
decval=11;
break;
case 'C':
case 'c':
decval=12;
break;
case 'D':
case 'd':
decval=13;
break;
case 'E':
case 'e':
decval=14;
break;
case 'F':
case 'f':
decval=15;
break;
default:
decval =0;
}
return decval;
}
int main() {
int maxhex=5;
char hex[maxhex+1];
char *bits;
printf("\n Hexadecimal to Binary Conversion");
printf("\n Enter hexadecimal value :") ;
scanf("%s",&hex);
for(int n=0;n<strlen(hex);n++) {
int decval = hex2dec(hex[n]);
char *b4s = dec2bin(decval);
strcat(bits,b4s);
//printf("\n %c \t %d \t %s",hex[n],decval,dec2bin(decval));
}
printf("\nHexadecimal value : %s", hex);
printf("\nBinary bits : %s ",bits);
return 0;
}
Hexadecimal to Binary conversion - C program output
Hexadecimal to Binary Conversion Enter hexadecimal value :a4c5 Hexadecimal value : a4c5 Binary bits :0000010000000101
Related post
- Decimal to binary using arithmetic operator
- Binary to decimal using arithmetic operator
- Decimal to 8 bit binary using arithmetic operator
- Decimal to binary using bitwise right-shift operator
- Binary to Decimal using bitwise right-shift operator
- Decimal to binary using bitwise left-shift operator
- Binary to Decimal using bitwise left-shift operator
- Octal to Binary number system conversion
- Binary to Octal number system conversion
- Binary to Hexadecimal number system conversion
showing segmentation fault for the above code
ReplyDelete