在C中打印char的二进制表示[duplicate]

时间:2022-10-25 00:16:24

This question already has an answer here:

这个问题在这里已有答案:

I want a really basic way to print out the binary representation of a char. I can't seem to find any example code anywhere.

我想要一种非常基本的方法来打印出char的二进制表示。我似乎无法在任何地方找到任何示例代码。

I assumed you could do it in a few lines but everything I find is overly long and complex using lots of functions I haven't used before. atoi comes up a lot but it's not standard.

我假设你可以在几行中完成它,但我发现的一切都过于冗长和复杂,使用了许多以前没用过的函数。 atoi出现了很多,但它不是标准的。

Is there a simple function or simple way of writing a function to take a char variable and then print out a binary representation?

是否有一个简单的函数或简单的方法来编写函数来获取char变量然后打印出二进制表示?

Eg: char 'x' is the argument taken in by the function and "x is 0111 1000" is printed out.

例如:char'x'是函数接收的参数,打印出“x is 0111 1000”。

It's for a school assignment where I must take user input of a string and print out the string in binary. I just need to get the basics of converting a char to binary but i'm struggling at the moment.

这是一个学校作业,我必须用户输入字符串并打印出二进制字符串。我只需要获得将char转换为二进制的基础知识,但此刻我正在努力。

4 个解决方案

#1


15  

What you'd want to do is use bitwise operators to mask the bits one by one and print them to the standard output.

你想要做的是使用按位运算符逐个屏蔽这些位并将它们打印到标准输出。

  1. A char in C is guaranteed to be 1 byte, so loop to 8.
  2. C中的char保证为1个字节,因此循环为8。

  3. Within each iteration, mask off the highest order bit.
  4. 在每次迭代中,屏蔽最高位。

  5. Once you have it, just print it to standard output.
  6. 一旦你拥有它,只需将其打印到标准输出。

Here is a quick stab which hopefully makes sense...

这是一个快速的刺,希望有意义......

main() {
  char a = 10;
  int i;
  for (i = 0; i < 8; i++) {
      printf("%d", !!((a << i) & 0x80));
  }
  printf("\n");

  return 0;
}

CodePad.

In order to get the bit, I shift to the left to get the numbered bit (highest to lowest so printing it is easy) and then mask it off. I then translate it to 0 or 1 with !!.

为了获得该位,我向左移动以获得编号位(从最高到最低,因此打印很容易),然后将其屏蔽掉。然后我用!!将它翻译成0或1。

#2


2  

you can use this method

你可以使用这种方法

const char *byte_to_binary(int x)
{
    static char b[9];
    b[0] = '\0';

    int z;
    for (z = 128; z > 0; z >>= 1)
    {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }

    return b;
}

to get the binary representation and print with it

获取二进制表示并使用它进行打印

for example

 printf("%s\n", byte_to_binary(15));

#3


1  

void printBits(size_t const size, void const * const ptr)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;

    for (i=size-1;i>=0;i--)
    {
        for (j=7;j>=0;j--)
        {
            byte = b[i] & (1<<j);
            byte >>= j;
            printf("%u", byte);
        }
    }
    puts("");
}

int main(int argv, char* argc[])
{
        int i = 23;
        uint ui = UINT_MAX;
        float f = 23.45f;
        printBits(sizeof(i), &i);
        printBits(sizeof(ui), &ui);
        printBits(sizeof(f), &f);
        return 0;
}

#4


0  

Try this:-

#include <limits.h>
char *chartobin ( unsigned char c )
{
    static char bin[CHAR_BIT + 1] = {0};
    int i;
    for( i = CHAR_BIT - 1; i >= 0; i-- )
    {
        bin[i] = (c % 2) + '0';
        c /= 2;
    }
   return bin;
}

#1


15  

What you'd want to do is use bitwise operators to mask the bits one by one and print them to the standard output.

你想要做的是使用按位运算符逐个屏蔽这些位并将它们打印到标准输出。

  1. A char in C is guaranteed to be 1 byte, so loop to 8.
  2. C中的char保证为1个字节,因此循环为8。

  3. Within each iteration, mask off the highest order bit.
  4. 在每次迭代中,屏蔽最高位。

  5. Once you have it, just print it to standard output.
  6. 一旦你拥有它,只需将其打印到标准输出。

Here is a quick stab which hopefully makes sense...

这是一个快速的刺,希望有意义......

main() {
  char a = 10;
  int i;
  for (i = 0; i < 8; i++) {
      printf("%d", !!((a << i) & 0x80));
  }
  printf("\n");

  return 0;
}

CodePad.

In order to get the bit, I shift to the left to get the numbered bit (highest to lowest so printing it is easy) and then mask it off. I then translate it to 0 or 1 with !!.

为了获得该位,我向左移动以获得编号位(从最高到最低,因此打印很容易),然后将其屏蔽掉。然后我用!!将它翻译成0或1。

#2


2  

you can use this method

你可以使用这种方法

const char *byte_to_binary(int x)
{
    static char b[9];
    b[0] = '\0';

    int z;
    for (z = 128; z > 0; z >>= 1)
    {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }

    return b;
}

to get the binary representation and print with it

获取二进制表示并使用它进行打印

for example

 printf("%s\n", byte_to_binary(15));

#3


1  

void printBits(size_t const size, void const * const ptr)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;

    for (i=size-1;i>=0;i--)
    {
        for (j=7;j>=0;j--)
        {
            byte = b[i] & (1<<j);
            byte >>= j;
            printf("%u", byte);
        }
    }
    puts("");
}

int main(int argv, char* argc[])
{
        int i = 23;
        uint ui = UINT_MAX;
        float f = 23.45f;
        printBits(sizeof(i), &i);
        printBits(sizeof(ui), &ui);
        printBits(sizeof(f), &f);
        return 0;
}

#4


0  

Try this:-

#include <limits.h>
char *chartobin ( unsigned char c )
{
    static char bin[CHAR_BIT + 1] = {0};
    int i;
    for( i = CHAR_BIT - 1; i >= 0; i-- )
    {
        bin[i] = (c % 2) + '0';
        c /= 2;
    }
   return bin;
}