{
int i;
char s[20];
clrscr();
printf("HEXADECIMAL TO BINARY\n");
printf("Enter A Hexadecimal Number:");
scanf("%s",s);
gets(s);
printf("Binary= ");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf("Number Is Not Hexadecimal");
break;
}
}
}
You're Browsing As A Guest! Register Now... |
||
|
Become a C++ Expert!
Join 416,724 C++ Programmers for FREE! Get instant access to thousands
of C++ experts, tutorials, code snippets, and more! There are 2,883 people online right now.Registration is fast and FREE... Join Now!
|
||
Page 1 of 1
Hexadecimal to Binary in C.. what's wrong? why does it doesn't give an answer?
#1
Hexadecimal to Binary in C.. what's wrong?
Posted 30 August 2007 - 07:37 PM
hey guys help me why does tihs program that will convert hexa to binary doesn't give an answer what's wrong in here:
#3
Re: Hexadecimal to Binary in C.. what's wrong?
Posted 31 August 2007 - 10:40 PM
scanf("%s",s);
gets(s);
You are trying to take input twice.
As a result, after the first Hex Number is entered, the gets() function does not take input but sets the first element in the buffer (in this case s[0]) to 0. Hence the String is considered empty since the '\0' character is the 0th element in the character array.
Remove either the gets() or scanf() function and the code will work as expected.
Page 1 of 1


Ask A New Question
Reply



MultiQuote








|