# Converting alphanumaric charecter in bianary

This is a discussion on Converting alphanumaric charecter in bianary within the C Programming forums, part of the General Programming Boards category; Pls tell me what logic I can use to convert a alphanumaric charecter in binary form....

1. ## Converting alphanumaric charecter in bianary

Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.

2. I'd suggest the 'search button' logic.

Quzah.

3. 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.

4. Code:
`char a = 'x'; /* x is now in binary form */`

5. Code:
`/*try*/ printf("100101011101011lOl1!001000001010110");`
Code:
```char str_to_convert[10];
int int_to_save_result;

atoi(str_to_convert, int_to_save_result, 2);```
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 bianary

6. Nope - consider the function prototype for the atoi function:

Code:
`int atoi(const char *nptr);`
It does not expect three arguments, just one.

Perhaps you were thinking of strtol? (Which I don't think will work the way you want either)

~/

7. 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.

8. >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.
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;
}```
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.

Cheers!