Does anyone know how to convert a character to the binary number in C. I need to get a character from the user and convert that character's value to the Binary number.
Chears
Coskun
Printable View
Does anyone know how to convert a character to the binary number in C. I need to get a character from the user and convert that character's value to the Binary number.
Chears
Coskun
You can use a recursive function to do that, though it's very easy to convert it to an iterative version... here's an example:
Code:void Bin( unsigned int n )
{
if( !n )
return;
Bin( n/2 );
printf( "%d", n%2 );
}
I don't think there's a standard library function that does that, but it's not difficult to write your own:
Code:#include <stdio.h>
int main(void)
{
int i, ch;
char binary[9], *b = &binary[7];
printf("Enter the character: ");
scanf("%c", (char *)&ch);
for(i = 1;b >= binary;i <<= 1)
*b-- = (ch & i)?'1':'0';
binary[8] = '\0';
puts(binary);
return 0;
}
Thanks alot itsme86 and Vber that information you have given to me was very useful thanks a lot.
Apparently noone knows how to use the search button, for this, one of the most common homework questions around...
Quzah.