我可以用printf打印十六进制或八进制数字。是否有格式标签打印为二进制或任意基数?
我正在运行gcc。
printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n"
printf("%b\n", 10); // prints "%b\n"
我可以用printf打印十六进制或八进制数字。是否有格式标签打印为二进制或任意基数?
我正在运行gcc。
printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n"
printf("%b\n", 10); // prints "%b\n"
当前回答
// m specifies how many of the low bits are shown.
// Replace m with sizeof(n) below for all bits and
// remove it from the parameter list if you like.
void print_binary(unsigned long n, unsigned long m) {
static char show[3] = "01";
unsigned long mask = 1ULL << (m-1);
while(mask) {
putchar(show[!!(n&mask)]); mask >>= 1;
}
putchar('\n');
}
其他回答
接下来将向您展示内存布局:
#include <limits>
#include <iostream>
#include <string>
using namespace std;
template<class T> string binary_text(T dec, string byte_separator = " ") {
char* pch = (char*)&dec;
string res;
for (int i = 0; i < sizeof(T); i++) {
for (int j = 1; j < 8; j++) {
res.append(pch[i] & 1 ? "1" : "0");
pch[i] /= 2;
}
res.append(byte_separator);
}
return res;
}
int main() {
cout << binary_text(5) << endl;
cout << binary_text(.1) << endl;
return 0;
}
根据@ideasman42在他的回答中的建议,这是一个提供int8、16,32和64版本的宏,重用int8宏以避免重复。
/* --- PRINTF_BYTE_TO_BINARY macro's --- */
#define PRINTF_BINARY_SEPARATOR
#define PRINTF_BINARY_PATTERN_INT8 "%c%c%c%c%c%c%c%c"
#define PRINTF_BYTE_TO_BINARY_INT8(i) \
(((i) & 0x80ll) ? '1' : '0'), \
(((i) & 0x40ll) ? '1' : '0'), \
(((i) & 0x20ll) ? '1' : '0'), \
(((i) & 0x10ll) ? '1' : '0'), \
(((i) & 0x08ll) ? '1' : '0'), \
(((i) & 0x04ll) ? '1' : '0'), \
(((i) & 0x02ll) ? '1' : '0'), \
(((i) & 0x01ll) ? '1' : '0')
#define PRINTF_BINARY_PATTERN_INT16 \
PRINTF_BINARY_PATTERN_INT8 PRINTF_BINARY_SEPARATOR PRINTF_BINARY_PATTERN_INT8
#define PRINTF_BYTE_TO_BINARY_INT16(i) \
PRINTF_BYTE_TO_BINARY_INT8((i) >> 8), PRINTF_BYTE_TO_BINARY_INT8(i)
#define PRINTF_BINARY_PATTERN_INT32 \
PRINTF_BINARY_PATTERN_INT16 PRINTF_BINARY_SEPARATOR PRINTF_BINARY_PATTERN_INT16
#define PRINTF_BYTE_TO_BINARY_INT32(i) \
PRINTF_BYTE_TO_BINARY_INT16((i) >> 16), PRINTF_BYTE_TO_BINARY_INT16(i)
#define PRINTF_BINARY_PATTERN_INT64 \
PRINTF_BINARY_PATTERN_INT32 PRINTF_BINARY_SEPARATOR PRINTF_BINARY_PATTERN_INT32
#define PRINTF_BYTE_TO_BINARY_INT64(i) \
PRINTF_BYTE_TO_BINARY_INT32((i) >> 32), PRINTF_BYTE_TO_BINARY_INT32(i)
/* --- end macros --- */
#include <stdio.h>
int main() {
long long int flag = 1648646756487983144ll;
printf("My Flag "
PRINTF_BINARY_PATTERN_INT64 "\n",
PRINTF_BYTE_TO_BINARY_INT64(flag));
return 0;
}
这个输出:
My Flag 0001011011100001001010110111110101111000100100001111000000101000
为了可读性,您可以更改:#define PRINTF_BINARY_SEPARATOR为#define PRINTF_BINARY_SEPARATOR ","或#define PRINTF_BINARY_SEPARATOR " "
这将输出:
My Flag 00010110,11100001,00101011,01111101,01111000,10010000,11110000,00101000
or
My Flag 00010110 11100001 00101011 01111101 01111000 10010000 11110000 00101000
c
// Based on https://stackoverflow.com/a/112956/1438550
#include <stdio.h>
#include <stdint.h>
const char *int_to_binary_str(int x, int N_bits){
static char b[512];
char *p = b;
b[0] = '\0';
for(int i=(N_bits-1); i>=0; i--){
*p++ = (x & (1<<i)) ? '1' : '0';
if(!(i%4)) *p++ = ' ';
}
return b;
}
int main() {
for(int i=31; i>=0; i--){
printf("0x%08X %s \n", (1<<i), int_to_binary_str((1<<i), 32));
}
return 0;
}
期望的行为:
Run:
gcc -pthread -Wformat=0 -lm -o main main.c; ./main
Output:
0x80000000 1000 0000 0000 0000 0000 0000 0000 0000
0x40000000 0100 0000 0000 0000 0000 0000 0000 0000
0x20000000 0010 0000 0000 0000 0000 0000 0000 0000
0x10000000 0001 0000 0000 0000 0000 0000 0000 0000
0x08000000 0000 1000 0000 0000 0000 0000 0000 0000
0x04000000 0000 0100 0000 0000 0000 0000 0000 0000
0x02000000 0000 0010 0000 0000 0000 0000 0000 0000
0x01000000 0000 0001 0000 0000 0000 0000 0000 0000
0x00800000 0000 0000 1000 0000 0000 0000 0000 0000
0x00400000 0000 0000 0100 0000 0000 0000 0000 0000
0x00200000 0000 0000 0010 0000 0000 0000 0000 0000
0x00100000 0000 0000 0001 0000 0000 0000 0000 0000
0x00080000 0000 0000 0000 1000 0000 0000 0000 0000
0x00040000 0000 0000 0000 0100 0000 0000 0000 0000
0x00020000 0000 0000 0000 0010 0000 0000 0000 0000
0x00010000 0000 0000 0000 0001 0000 0000 0000 0000
0x00008000 0000 0000 0000 0000 1000 0000 0000 0000
0x00004000 0000 0000 0000 0000 0100 0000 0000 0000
0x00002000 0000 0000 0000 0000 0010 0000 0000 0000
0x00001000 0000 0000 0000 0000 0001 0000 0000 0000
0x00000800 0000 0000 0000 0000 0000 1000 0000 0000
0x00000400 0000 0000 0000 0000 0000 0100 0000 0000
0x00000200 0000 0000 0000 0000 0000 0010 0000 0000
0x00000100 0000 0000 0000 0000 0000 0001 0000 0000
0x00000080 0000 0000 0000 0000 0000 0000 1000 0000
0x00000040 0000 0000 0000 0000 0000 0000 0100 0000
0x00000020 0000 0000 0000 0000 0000 0000 0010 0000
0x00000010 0000 0000 0000 0000 0000 0000 0001 0000
0x00000008 0000 0000 0000 0000 0000 0000 0000 1000
0x00000004 0000 0000 0000 0000 0000 0000 0000 0100
0x00000002 0000 0000 0000 0000 0000 0000 0000 0010
0x00000001 0000 0000 0000 0000 0000 0000 0000 0001
glibc中通常没有二进制转换说明符。
在glibc中,可以向printf()函数家族添加自定义转换类型。有关详细信息,请参阅register_printf_function。如果可以简化应用程序代码,您可以添加自定义%b转换供自己使用。
下面是如何在glibc中实现自定义printf格式的示例。
下面是paniq解决方案的一个小变种,它使用模板来允许打印32位和64位整数:
template<class T>
inline std::string format_binary(T x)
{
char b[sizeof(T)*8+1] = {0};
for (size_t z = 0; z < sizeof(T)*8; z++)
b[sizeof(T)*8-1-z] = ((x>>z) & 0x1) ? '1' : '0';
return std::string(b);
}
并且可以这样使用:
unsigned int value32 = 0x1e127ad;
printf( " 0x%x: %s\n", value32, format_binary(value32).c_str() );
unsigned long long value64 = 0x2e0b04ce0;
printf( "0x%llx: %s\n", value64, format_binary(value64).c_str() );
结果如下:
0x1e127ad: 00000001111000010010011110101101
0x2e0b04ce0: 0000000000000000000000000000001011100000101100000100110011100000