Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.
Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.
I'd suggest the 'search button' logic.
Quzah.
Hope is the first step on the road to disappointment.
a) It's all binary inside the machine
b) convert it to what
c) at least post some examples so we have some idea of what you want.
Code:char a = 'x'; /* x is now in binary form */
Code:/*try*/ printf("100101011101011lOl1!001000001010110");the atoi function takes a string or character array and turns all elements into one long integer with the base of the last argument. 2 meaning that it would turn the values into bianaryCode:char str_to_convert[10]; int int_to_save_result; atoi(str_to_convert, int_to_save_result, 2);
Last edited by mako; 01-07-2006 at 07:00 AM.
Nope - consider the function prototype for the atoi function:
It does not expect three arguments, just one.Code:int atoi(const char *nptr);
Perhaps you were thinking of strtol? (Which I don't think will work the way you want either)
~/
Last edited by kermit; 01-07-2006 at 07:39 AM.
Maybe it was the non-standard itoa function.
Besides which, the buffer was woefully too small for 32 bits (and a \0) of potential binary data stored in your typical 32-bit integer.
>the atoi function takes a string or character array and turns all
>elements into one long integer with the base of the last argument.
My compiler's atoi only takes a string, and only handles base 10. The C standard seems to agree with that. I'd wager that you're using a non-standard extension and you need to be sure that the person you're helping has the same compiler as you do before suggesting it.
>Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.
The simplest way, in this case, is probably to do it manually. Once you get your head around the bit twiddling stuff, it's pretty neat.
The expression funky bitwise expression is supposed to shift the bit that I want into the first position and test whether it's non-zero or not, then replace it with '0' or '1' accordingly. Since I start--theoretically--at the highest bit for the logical type and count down, that results in printing out the bit representation of the value. I used unsigned long as the type for x because how many bits the function prints depends on the bits argument, so to_binary() can be way more generic than if I just make x an char.Code:#include <stdio.h> #include <limits.h> void to_binary ( unsigned long x, int bits ) { while ( --bits >= 0 ) putchar ( ( x >> bits & 1 ) ? '1' : '0' ); } int main ( void ) { to_binary ( 'A', CHAR_BIT ); return 0; }
Cheers!