C 中字符到二进制的转换
我正在尝试将字符转换为其二进制表示形式(因此字符 --> ascii 十六进制 --> 二进制)。
我知道要做到这一点,我需要移动和AND
。但是,由于某种原因,我的代码无法正常工作。
这是我所拥有的。 *temp
指向 C 字符串中的索引。
char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
char x = *temp;
c = (x >> i) & 1;
printf("%d\n", c);
temp--;
}
I am trying to convert a character to its binary representation (so character --> ascii hex --> binary).
I know to do that I need to shift and AND
. However, my code is not working for some reason.
Here is what I have. *temp
points to an index in a C string.
char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
char x = *temp;
c = (x >> i) & 1;
printf("%d\n", c);
temp--;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们展示了两个将单个字符打印为二进制的函数。
printbinchar(10) 将写入控制台
itoa 是一个库函数,它将单个整数值转换为具有指定基数的字符串。
例如... itoa(1341, output, 10) 将写入输出字符串“1341”。
当然 itoa(9, output, 2) 将写入输出字符串“1001”。
下一个函数将把字符的完整二进制表示形式打印到标准输出中,也就是说,它将打印所有 8 位,即使高位为零。
printbincharpad(10) 将写入控制台
现在我展示一个打印出整个字符串(没有最后一个空字符)的函数。
但请考虑 itoa 不会添加填充零,因此 printstringasbinary("AB1") 将打印如下内容:
We show up two functions that prints a SINGLE character to binary.
printbinchar(10) will write into the console
itoa is a library function that converts a single integer value to a string with the specified base.
For example... itoa(1341, output, 10) will write in output string "1341".
And of course itoa(9, output, 2) will write in the output string "1001".
The next function will print into the standard output the full binary representation of a character, that is, it will print all 8 bits, also if the higher bits are zero.
printbincharpad(10) will write into the console
Now i present a function that prints out an entire string (without last null character).
Consider however that itoa don't adds padding zeroes, so printstringasbinary("AB1") will print something like:
说明:
每次迭代时,都会通过移位并与 1 进行二进制比较来从字节中读取最高有效位。
例如,假设输入值为 128,二进制转换为 1000 0000。
将其移位 7 将得到 0000 0001,因此得出结论,最高有效位是 1. 0000 0001 & 1. 0000 0001。 1 = 1。这是在控制台中打印的第一位。下一次迭代将得到 0 ... 0。
Explanation:
With every iteration, the most significant bit is being read from the byte by shifting it and binary comparing with 1.
For example, let's assume that input value is 128, what binary translates to 1000 0000.
Shifting it by 7 will give 0000 0001, so it concludes that the most significant bit was 1. 0000 0001 & 1 = 1. That's the first bit to print in the console. Next iterations will result in 0 ... 0.
您的代码非常模糊且难以理解,但我可以为您提供替代方案。
首先,如果您希望
temp
遍历整个字符串,您可以执行以下操作:术语
*temp
作为for
条件只是检查是否到达字符串末尾。如果有,*temp
将是'\0'
(NUL
) 并且for
结束。现在,在 for 内部,您想要查找组成
*temp
的位。假设我们打印位:为了使其更通用,即将任何类型转换为位,您可以将
bit_index = 7
更改为bit_index = sizeof(*temp)* 8-1
Your code is very vague and not understandable, but I can provide you with an alternative.
First of all, if you want
temp
to go through the whole string, you can do something like this:The term
*temp
as thefor
condition simply checks whether you have reached the end of the string or not. If you have,*temp
will be'\0'
(NUL
) and thefor
ends.Now, inside the for, you want to find the bits that compose
*temp
. Let's say we print the bits:To make it a bit more generic, that is to convert any type to bits, you can change the
bit_index = 7
tobit_index = sizeof(*temp)*8-1