我想获得 C: int
中所有类型变量的二进制表示形式, unsigned int
, long
, unsigned long
, short
, unsigned short
, float
, double
和char
。
当我获取变量的大小(通过 sizeof()
)并将其转换为二进制系统时,这是最好的解决方案吗?
如何快速、轻松地进行这样的转换?
最佳答案
获取任何类型的二进制表示:
我编写了以下代码来提供两个函数来完成您的要求。第一个函数getBinaryHostMemoryRepresentation()
将用它们在主机内存中保存的方式填充您提供的缓冲区
:这可能是小尾数、大尾数或其他(不太可能)。例如,整数 0xABCD
在小端上保存为 0xCDAB
。
getBinaryRepresentation()
函数将检查您正在运行的机器,并用您期望的人类可读的大端字节表示形式填充您的缓冲区
所以你会得到0xABCD
。要检查程序正在运行的机器类型,请使用 isLittleEndian()
函数,查看它是如何工作的 here on SO .
这两个函数都需要以下参数:char * const buffer、size_t bufSize、const void * var、size_t varSize
。前两个是缓冲区和缓冲区的大小。最后两个是要转换的变量(它是一个空指针,可以是任何数据)和可以通过 sizeof()
获取的变量的大小(以字节为单位)。此外,函数检查缓冲区是否足够大,如果不够大,它们将返回空指针。如果它足够大,它们将返回指向您提供的缓冲区的指针。可以在 main()
函数中观察其用法。
main()
函数对上述函数使用两个输入:
- 整数:
2882400235
,即0xABCDEFEB
或0b10101011110011011110111111101011
- float :
42.0
,即0x42280000
或0b01000010001010000000000000000000
。
要检查整数,您可以使用 Windows 计算器,要检查 float ,您可以使用 this site online .
程序的输出:
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;
}
关于c - 获取 C 中变量的二进制表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530187/