How to convert hexadecimal to binary using c language..



How to convert hexadecimal to binary using c language....

Answer / nipun

#include <stdio.h>
#include <math.h>
#include <string.h>
void binary_hex(int n, char hex[]);
int hex_binary(char hex[]);
int main()
{
char hex[20],c;
int n;
printf("\nInstructions:\n");
printf("Enter h to convert binary to hexadecimal:\n");
printf("Enter b to hexadecimal number to binary:\n");
printf("Enter a character: ");
scanf("%c",&c);
if (c=='h' || c=='H')
{
printf("Enter binary number: ");
scanf("%d",&n);
binary_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c=='b' || c=='B')
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Binary number: %d",hex_binary(hex));
}
return 0;
}

void binary_hex(int n, char hex[]) /* Function to convert
binary to hexadecimal. */
{
int i=0,decimal=0, rem;
while (n!=0)
{
decimal += (n%10)*pow(2,i);
n/=10;
++i;
}

/* At this point, variable decimal contains binary number in
decimal format. */
i=0;
while (decimal!=0)
{
rem=decimal%16;
switch(rem)
{
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
default:
hex[i]=rem+'0';
break;
}
++i;
decimal/=16;
}
hex[i]='\0';
strrev(hex); /* Function to reverse string. */
}

int hex_binary(char hex[]) /* Function to convert
hexadecimal to binary. */
{
int i, length, decimal=0, binary=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
}
/* At this point, variable decimal contains the hexadecimal
number in decimal format. */

i=1;
while (decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C C++ Errors Interview Questions

what is exceptions?

5 Answers   HCL, Wipro,


Write down the difference between c. Loop and goto statement d. (!0) and (!1) e. (1= =! 1) and (1!=1) f. NULL and !NULL

0 Answers  


what is the error in the following code: main() { int i=400,j; j=(i*i)/i; }

4 Answers  


how tally is useful?

2 Answers  


void main() { for(int i=0;i<5;i++); printf("%d",i); } What is the output?..

32 Answers   College School Exams Tests, CTS, HCL, iGate, SmartData,






What is the code for following o/p * * * * * * * * * * * * * * * *

1 Answers  


main() { char c; for(c='A';c<='Z';c++) getch(); }

9 Answers  


wap for bubble sort

3 Answers  


which typw of errors ? & how to solve it ?

0 Answers  


Write a C program to enter 10 integer numbers through one variable and count how many of them are even using while loop ?

2 Answers  


How to convert hexadecimal to binary using c language..

1 Answers   Bajaj, GAIL, Satyam, Zenqa,


I'm having trouble with coming up with the correct code. Do I need to put a loop? Please let me know if I'm on the right track and what areas I need to correct. I still don't have a good grasp on this programming stuff. Thanks =) The assignment was to write a program using string functions that accepts a coded value of an item and displays its equivalent tag price. The base of the keys: 0 1 2 3 4 5 6 7 8 9 X C O M P U T E R S Sample I/O Dialogue: Enter coded value: TR.XX Tag Price : 68.00

3 Answers   UCB,


Categories