# char to binary conversion

• 08-31-2005
Niara
char to binary conversion
hello, I have a question about how to convert a single character to its binary equivalent. I have find the way to do it for integer numbers:

Code:

```int c=8; while(c>0) {cout<<c%2;c=c/2;}```
to show the 'reversed' binary. also I have find the way to do it with plain characters, only get its ascii and do the same as before, so:

Code:

```int bin[8]; int pe; char c='a'; printf("dec:%d\n",c); pe=7; while(c>0)     {     bin[pe]=c%2;     c=c/2;     pe--;     } for(int np=pe;np>=0;np--) {bin[np]=0;} printf("bin:"); for(int q=0;q<8;q++) {printf("%d",bin[q]);} printf("\n");```
but how can I convert an accentuated character to its binary representation? the method of convert its ascii number does not work here. also seems not to work here the loop though a
while(c<0)
How can I do it?
Niara
• 08-31-2005
hk_mp5kpdw
A bitset is very easy to use:

Code:

```#include <bitset> #include <iostream> ... char c = 'A'; // Convert character into bitset std::bitset<8> bits(static_cast<unsigned long>(c)); // Display bitset std::cout << bits << std::endl;```
On my machine this outputs: 01000001
• 08-31-2005
Tonto
http://cboard.cprogramming.com/searc...earchid=218529

You'll note that this is quite a frequent subject with many solutions. Filter through the binary search tree and binary file writing results and that search will have many helpful replies with example source you might like.
• 09-01-2005
Niara
hello both, thank's for your time and help.
hk_mp5kpdw: that function gives exactly what I was trying to do, so I will try to find a bit more info about related functions and functionality.
Tonto: yes, I never used the searcher; so I'll be to take into account before ask new problems, I know that a previous search can help me on orientating my doubts to some exact interest points.
Niara