【问题标题】:Conversion of Char to Binary in C在 C 中将字符转换为二进制
【发布时间】:2011-12-13 09:24:13
【问题描述】:

我正在尝试将字符转换为其二进制表示形式(因此字符 --> 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--;
}

【问题讨论】:

  • 什么是ptrPos,什么是i
  • 听起来您正在尝试将 single 字符转换为二进制,在这种情况下,这相当简单,意味着您不需要指针(那不会没有任何意义),您只需要移动位并检查它是否为 1(与 1 相加)。看看 Salvatore 的第二个 sn-p。
  • 你能给我们展示一两个具体输入和输出的例子吗?

标签: c string binary char ascii


【解决方案1】:
unsigned char c;

for( int i = 7; i >= 0; i-- ) {
    printf( "%d", ( c >> i ) & 1 ? 1 : 0 );
}

printf("\n");

说明:

在每次迭代中,最高有效位是通过移位字节并与 1 进行二进制比较来读取的。

例如,假设输入值为 128,二进制转换为 1000 0000。 将其移动 7 将得到 0000 0001,因此得出的结论是最高有效位是 1. 0000 0001 & 1 = 1。这是在控制台中打印的第一位。下一次迭代将产生 0 ... 0。

【讨论】:

    【解决方案2】:

    您的代码非常模糊且难以理解,但我可以为您提供替代方案。

    首先,如果你想让temp 遍历整个字符串,你可以这样做:

    char *temp;
    for (temp = your_string; *temp; ++temp)
        /* do something with *temp */
    

    *temp 作为for 条件的术语只是检查您是否已到达字符串的末尾。如果有,*temp 将是 '\0' (NUL),for 结束。

    现在,在 for 中,您想要找到构成 *temp 的位。假设我们打印这些位:

    for (as above)
    {
        int bit_index;
        for (bit_index = 7; bit_index >= 0; --bit_index)
        {
            int bit = *temp >> bit_index & 1;
            printf("%d", bit);
        }
        printf("\n");
    }
    

    为了使其更通用一点,即将任何类型转换为位,您可以将bit_index = 7 更改为bit_index = sizeof(*temp)*8-1

    【讨论】:

      【解决方案3】:

      我们展示了两个将单个字符打印为二进制的函数。

      void printbinchar(char character)
      {
          char output[9];
          itoa(character, output, 2);
          printf("%s\n", output);
      }
      

      printbinchar(10) 将写入控制台

          1010
      

      itoa 是一个库函数,可将单个整数值转换为具有指定基数的字符串。 例如... itoa(1341, output, 10) 将写入输出字符串“1341”。 当然 itoa(9, output, 2) 会写入输出字符串“1001”。

      下一个函数会将一个字符的完整二进制表示打印到标准输出中,也就是说,它将打印所有 8 位,如果高位为零。

      void printbincharpad(char c)
      {
          for (int i = 7; i >= 0; --i)
          {
              putchar( (c & (1 << i)) ? '1' : '0' );
          }
          putchar('\n');
      }
      

      printbincharpad(10) 将写入控制台

          00001010
      

      现在我提出一个打印出整个字符串(没有最后一个空字符)的函数。

      void printstringasbinary(char* s)
      {
          // A small 9 characters buffer we use to perform the conversion
          char output[9];
      
          // Until the first character pointed by s is not a null character
          // that indicates end of string...
          while (*s)
          {
              // Convert the first character of the string to binary using itoa.
              // Characters in c are just 8 bit integers, at least, in noawdays computers.
              itoa(*s, output, 2);
      
              // print out our string and let's write a new line.
              puts(output);
      
              // we advance our string by one character,
              // If our original string was "ABC" now we are pointing at "BC".
              ++s;
          }
      }
      

      但请考虑 itoa 不添加填充零,因此 printstringasbinary("AB1") 将打印如下内容:

      1000001
      1000010
      110001
      

      【讨论】:

      • 技术上应该是i = CHAR_BIT - 1。你知道,对于所有那些 9 位和 11 位处理器。
      • 这是错误的。 itoa 接受一个整数,而您传递的是 *s,一个字符。
      • 不是错误。 *s 表示字符串 s 指向的第一个字符。在 c 中,字符只是 8 位整数(现在)。
      • itoa(10, output, 2) 会将字符串“1010”写入输出字符串。
      • printbincharpad(10) 会将字符串“00001010”写入控制台。
      猜你喜欢
      • 2014-11-07
      • 1970-01-01
      • 2016-07-13
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多