如何将一个字符转换为二进制? [英] how to convert a char to binary?
问题描述
有一个字符转换为其二进制重新presentation一个简单的方法?
is there a simple way to convert a character to its binary representation?
我试着去在同一时间将消息发送到另一个进程,一个位。
因此,如果消息是你好,我需要首先把'H'成二进制,然后才能发送位。
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.
在一个数组中存储将是preferred。
Storing in an array would be preferred.
感谢您的任何反馈,无论是伪code或实际code将是最有帮助的。
Thanks for any feedback, either pseudo code or actual code would be the most helpful.
我想我应该提到这是一个作业,以了解信号......它只是为了了解他们一个有趣的方式。 SIGUSR1用作0,SIGUSR2用作1,并且点是从一个过程发送消息到另一个,pretending环境被锁定的通信的其他方法。
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.
推荐答案
您只循环每一位做一个转变,做一个逻辑和
来得到位
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);
}
例如:
unsigned char mychar = 0xA5; // 10100101
(mychar >> 0) 10100101
& 1 & 00000001
============= 00000001 (bit 1)
(mychar >> 1) 01010010
& 1 & 00000001
============= 00000000 (bit 0)
等等...
这篇关于如何将一个字符转换为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!