Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取C中变量的二进制表示形式_C - Fatal编程技术网

获取C中变量的二进制表示形式

获取C中变量的二进制表示形式,c,C,我想得到C中所有类型变量的二进制表示:int,无符号int,long,无符号long,short,无符号short,float,double和char 当我获取变量的大小(通过sizeof())并将其转换为二进制时,这是最好的解决方案吗 如何快速方便地进行这种转换?获取任何类型的二进制表示: 我编写了以下代码来提供两个函数,它们可以满足您的要求。第一个函数getBinaryHostMemoryRepresentation()将用存储在主机内存中的位填充提供的缓冲区:可能是小端、大端或其他(不太可

我想得到C中所有类型变量的二进制表示:
int
无符号int
long
无符号long
short
无符号short
float
double
char

当我获取变量的大小(通过
sizeof()
)并将其转换为二进制时,这是最好的解决方案吗


如何快速方便地进行这种转换?

获取任何类型的二进制表示:

我编写了以下代码来提供两个函数,它们可以满足您的要求。第一个函数
getBinaryHostMemoryRepresentation()
将用存储在主机内存中的位填充提供的
缓冲区
:可能是小端、大端或其他(不太可能)。例如,整数
0xCDAB
0xCDAB
的形式保存在一个小端点上

getBinaryRepresentation()
函数将检查您正在运行的机器,并将使用您期望为人类可读的位表示法填充您的
缓冲区
,该位表示法是大端的,因此您将获得
0xABCD
。要检查程序正在运行的机器类型,请使用
isLittleEndian()
函数,查看其工作原理

这两个函数都需要以下参数:
char*const buffer、size\t bufSize、const void*var、size\t varSize
。前两个是缓冲区和缓冲区的大小。最后两个是要转换的变量(它是一个空指针,可以是任何数据)和变量的大小(以字节为单位),可以通过
sizeof()
获得。此外,函数检查缓冲区是否足够大,如果不够大,则返回空指针。如果缓冲区足够大,则返回指向您提供的缓冲区的指针。可以在
main()
函数中观察使用情况

main()
函数对上述函数使用两个输入:

  • 整数:
    2882400235
    它是
    0xABCDEFEB
    0b101010111100111111111101011
  • 浮点数:
    42.0
    ,即
    0x42280000
    0B0100001000010000000000000000000000000000
  • 要检查整数,可以使用Windows计算器;要检查浮点,可以使用

    程序的输出:

    integer 2882400235:
    Host memory binary representation:    "11101011111011111100110110101011"
    Human readable binary representation: "10101011110011011110111111101011"
    
    single floating point 42.0:
    Host memory binary representation:    "00000000000000000010100001000010"
    Human readable binary representation: "01000010001010000000000000000000"
    

    在用C标记问题时,使用C编写代码:

    #include <stdio.h>
    #include <stddef.h>
    #include <limits.h>
    
    #if CHAR_BIT != 8
    #error "unsupported char size"
    #endif
    
    int isLittleEndian ()
    {
       static const int num = 1;
       if (1 == *((char *) &num))
       {
          return 1;
       }
       else
       {
          return 0;
       }
    }
    
    char * getBinaryHostMemoryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize)
    {
       size_t byteIdx;
       size_t bitIdx;
    
       if (bufSize < varSize * CHAR_BIT + 1)
       {
          return NULL;
       }
       const unsigned char * curByte = (const unsigned char *) var;
       for (byteIdx = 0; byteIdx < varSize; ++byteIdx, ++curByte)
       {
          for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx)
          {
             unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx);
             buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0';
          }
       }
       buffer[varSize * CHAR_BIT] = '\0';
       return buffer;
    }
    
    char * getBinaryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize)
    {
       size_t byteIdx;
       size_t bitIdx;
    
       if (bufSize < varSize * CHAR_BIT + 1)
       {
          return NULL;
       }
    
       const unsigned char * curByte;;
       int incByte;
       if (isLittleEndian ())
       {
          curByte = (const unsigned char *) var + (varSize - 1);
          incByte = -1;
       }
       else
       {
          curByte = (const unsigned char *) var;
          incByte = 1;
       }
       for (byteIdx = 0; byteIdx < varSize; ++byteIdx, curByte += incByte)
       {
          for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx)
          {
             unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx);
             buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0';
          }
       }
       buffer[varSize * CHAR_BIT] = '\0';
       return buffer;
    }
    
    int main ()
    {
       int integer = 2882400235; /* 10101011110011011110111111101011 */
       char bufferMemInt[sizeof (integer) * CHAR_BIT + 1];
       char bufferBinInt[sizeof (integer) * CHAR_BIT + 1];
    
       printf ("integer 2882400235:\n");
       if (getBinaryHostMemoryRepresentation (bufferMemInt,
                                              sizeof (bufferMemInt),
                                              (void *) &integer,
                                              sizeof (integer)))
       {
          printf ("Host memory binary representation:    \"%s\"",
                  bufferMemInt);
          printf ("\n");
       }
    
       if (getBinaryRepresentation (bufferBinInt,
                                    sizeof (bufferBinInt),
                                    (void *) &integer,
                                    sizeof (integer)))
       {
          printf ("Human readable binary representation: \"%s\"",
                  bufferBinInt);
          printf ("\n");
       }
    
    
       float floating = 42.0; /* 01000010001010000000000000000000 */
       char bufferMemFloat[sizeof (floating) * CHAR_BIT + 1];
       char bufferBinFloat[sizeof (floating) * CHAR_BIT + 1];
    
       printf ("\n");
       printf ("single floating point 42.0:\n");
       if (getBinaryHostMemoryRepresentation (bufferMemFloat,
                                              sizeof (bufferMemFloat),
                                              (void *) &floating,
                                              sizeof (floating)))
       {
          printf ("Host memory binary representation:    \"%s\"",
                  bufferMemFloat);
          printf ("\n");
       }
    
       if (getBinaryRepresentation (bufferBinFloat,
                                    sizeof (bufferBinFloat),
                                    (void *) &floating,
                                    sizeof (floating)))
       {
          printf ("Human readable binary representation: \"%s\"",
                  bufferBinFloat);
          printf ("\n");
       }
    
       return 0;
    }
    
    #包括
    #包括
    #包括
    #如果字符位!=8.
    #错误“不支持的字符大小”
    #恩迪夫
    int isLittleEndian()
    {
    静态常量int num=1;
    如果(1==*((字符*)&num))
    {
    返回1;
    }
    其他的
    {
    返回0;
    }
    }
    char*getBinaryHostMemoryRepresentation(char*const buffer,size\t bufSize,const void*var,size\t varSize)
    {
    大小按TEIDX;
    尺寸_tbitdx;
    if(bufSize((字符位-1)-bitIdx);
    缓冲区[byteIdx*CHAR_BIT+bitIdx]=corbit+'0';
    }
    }
    缓冲区[varSize*CHAR_位]='\0';
    返回缓冲区;
    }
    char*getBinaryRepresentation(char*const buffer,size\t bufSize,const void*var,size\t varSize)
    {
    大小按TEIDX;
    尺寸_tbitdx;
    if(bufSize((字符位-1)-bitIdx);
    缓冲区[byteIdx*CHAR_BIT+bitIdx]=corbit+'0';
    }
    }
    缓冲区[varSize*CHAR_位]='\0';
    返回缓冲区;
    }
    int main()
    {
    整数=2882400235;/*10101011110011011111111111101011*/
    char bufferMemInt[sizeof(整数)*char_位+1];
    char bufferBinInt[sizeof(整数)*字符位+1];
    printf(“整数2882400235:\n”);
    如果(getBinaryHostMemoryRepresentation)(bufferMemInt,
    sizeof(bufferMemInt),
    (void*)和整数,
    sizeof(整数)))
    {
    printf(“主机内存二进制表示:\%s\”,
    bufferMemInt);
    printf(“\n”);
    }
    if(getBinaryRepresentation)(bufferBinInt,
    sizeof(bufferBinInt),
    (void*)和整数,
    sizeof(整数)))
    {
    printf(“人类可读的二进制表示:\%s\”,
    bufferBinInt);
    printf(“\n”);
    }
    浮点数浮点数=42.0;/*0100001000010000000000000000000000000000000000*/
    char bufferMemFloat[sizeof(floating)*char_位+1];
    char bufferBinFloat[sizeof(floating)*char_位+1];
    printf(“\n”);
    printf(“单浮点42.0:\n”);
    如果(getBinaryHostMemoryRepresentation)(bufferMemFloat,
    sizeof(bufferMemFloat),
    (无效*)和浮动,
    sizeof(浮动)))
    {
    printf(“主机内存二进制表示:\%s\”,
    缓冲区(内存浮动);
    printf(“\n”);
    }
    如果(getBinaryRepresentation)(bufferBinFloat,
    sizeof(bufferBinFloat),
    (无效*)和浮动,
    sizeof(浮动)))
    {
    printf(“人类可读的二进制表示:\%s\”,