7

is there a simple way to convert a character to its binary representation?

Im trying to send a message to another process, a single bit at a time. So if the message is "Hello", i need to first turn 'H' into binary, and then send the bits in order.

Storing in an array would be preferred.

Thanks for any feedback, either pseudo code or actual code would be the most helpful.

I think I should mention this is for a school assignment to learn about signals... it's just an interesting way to learn about them. SIGUSR1 is used as 0, SIGUSR2 is used as 1, and the point is to send a message from one process to another, pretending the environment is locking down other methods of communication.

Blackbinary
  • 3,936
  • 18
  • 49
  • 62
  • Could you give an example of what you expect to actually be sent? – Anon. Feb 03 '11 at 22:42
  • So you want to send only a single binary digit at a time? Why? – Ed S. Feb 03 '11 at 22:42
  • well its with signals actually... all I need is the binary representation of a character. so perhaps i provide an int array, and through some function, it becomes the binary. then i can cycle through the array and send each bit. This is kind of an exercise in thinking out of the box. – Blackbinary Feb 03 '11 at 22:43
  • 5
    Would that box be labeled "sanity"? j/k, j/k :) – Ed S. Feb 03 '11 at 22:45
  • 1
    @David Heffernan, I think that is a mean spirited comment. I can understand this is certainly not the conventional way of doing things, but I think you could have simply said that there is a much easier way of doing things, or asking Why as Ed S. did. – Blackbinary Feb 03 '11 at 22:59
  • @Blackbinary Maybe so. But you really need to get on top of the basics. Do you have any text books? You'll learn quicker there than asking questions on SO. – David Heffernan Feb 03 '11 at 23:03
  • @David Heffernan, I disagree. SO is great for questions where I have the general idea, and I just need help with bits or pieces. In this case, our professor mentioned using a bit shift, but I wasn't sure if that was the best method, nor was I 100% on implementing it. And I try to avoid buying textbooks where I can. – Blackbinary Feb 03 '11 at 23:05
  • Can't you guys provide him with a better idea instead of deriding him? We were all a bit green once (and I probably still am.) – Skurmedel Feb 03 '11 at 23:11
  • you'd learn more if you read some books and tried harder before asking here – David Heffernan Feb 03 '11 at 23:20

2 Answers2

13

You have only to loop for each bit do a shift and do an logic AND to get the bit.

for (int i = 0; i < 8; ++i) {
    send((mychar >> i) & 1);
}

For example:

unsigned char mychar = 0xA5; // 10100101

(mychar >> 0)    10100101
& 1            & 00000001
=============    00000001 (bit 1)

(mychar >> 1)    01010010
& 1            & 00000001
=============    00000000 (bit 0)

and so on...

Murilo Vasconcelos
  • 4,677
  • 24
  • 27
3

What about:

int output[CHAR_BIT];
char c;
int i;
for (i = 0; i < CHAR_BIT; ++i) {
  output[i] = (c >> i) & 1;
}

That writes the bits from c into output, least significant bit first.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78