C语言 在 C 中打印字符的二进制表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18327439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:12:57  来源:igfitidea点击:

Printing binary representation of a char in C

cprintingbinarychar

提问by user1783150

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.

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

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. atoicomes 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.

这是一个学校作业,我必须接受用户输入的字符串并以二进制形式打印出字符串。我只需要了解将字符转换为二进制的基础知识,但目前我正在苦苦挣扎。

回答by alex

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 charin C is guaranteed to be 1 byte, so loop to 8.
  2. Within each iteration, mask off the highest order bit.
  3. Once you have it, just print it to standard output.
  1. charC 中的A保证为 1 个字节,因此循环到8.
  2. 在每次迭代中,屏蔽掉最高位。
  3. 获得后,只需将其打印到标准输出即可。

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 0or 1with !!.

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

回答by No Idea For Name

you can use this method

你可以用这个方法

const char *byte_to_binary(int x)
{
    static char b[9];
    b[0] = '
 printf("%s\n", byte_to_binary(15));
'; 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

例如

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;
}

回答by Optimus Prime

#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;
}

回答by Rahul Tripathi

Try this:-

尝试这个:-

##代码##