Turn your Mobile Apps into m-commerce apps – Learn More!

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!



Hexadecimal to Binary in C.. what's wrong? why does it doesn't give an answer? Rate Topic: -----

#1 arem026  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 28-August 07


Dream Kudos: 0

Share |

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:
{
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;
}
}
}

Was This Post Helpful? 1


#2 Xing  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 16
  • View blog
  • Posts: 725
  • Joined: 22-July 06


Dream Kudos: 1575

Re: Hexadecimal to Binary in C.. what's wrong?

Posted 30 August 2007 - 08:26 PM

Post at least one complete compilation unit with code tags.

This post has been edited by Xing: 30 August 2007 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 143
  • View blog
  • Posts: 4,636
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

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.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users