我可以用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"
当前回答
打印任何数据类型的二进制
// Assumes little endian
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] >> j) & 1;
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;
}
其他回答
以下是我对这个问题的看法。
与大多数其他例子相比的优点:
使用putchar(),它比printf()更有效,甚至(尽管没有那么多)puts() 分成两部分(预计有内联代码),如果需要,可以提高效率。 基于非常快速的RISC算术运算(包括不使用除法和乘法)
大多数例子的缺点:
代码不是很简单。 Print_binary_size()在不复制的情况下修改输入变量。
注意:此代码的最佳结果依赖于在gcc中使用-O1或更高的值或等效值。
代码如下:
inline void print_binary_sized(unsigned int number, unsigned int digits) {
static char ZERO = '0';
int digitsLeft = digits;
do{
putchar(ZERO + ((number >> digitsLeft) & 1));
}while(digitsLeft--);
}
void print_binary(unsigned int number) {
int digitsLeft = sizeof(number) * 8;
while((~(number >> digitsLeft) & 1) && digitsLeft){
digitsLeft--;
}
print_binary_sized(number, digitsLeft);
}
是否有一个printf转换器打印二进制格式?
没有标准的printf格式说明符来完成“二进制”输出。这是我在需要的时候设计的替代方案。
我的适用范围从2到36。它将数字扇出到递归调用的调用帧中,直到它到达一个比基数小的数字。然后它向后“遍历”,向前填充缓冲区,然后返回。返回值是使用的大小,如果缓冲区不够大,则返回-1。
int conv_rad (int num, int rad, char *s, int n) {
char *vec = "0123456789" "ABCDEFGHIJKLM" "NOPQRSTUVWXYZ";
int off;
if (n == 0) return 0;
if (num < rad) { *s = vec[num]; return 1; }
off = conv_rad(num/rad, rad, s, n);
if ((off == n) || (off == -1)) return -1;
s[off] = vec[num%rad];
return off+1;
}
一个重要的警告:这个函数是为“Pascal”风格的字符串设计的,它可以携带自己的长度。因此,如前所述,conv_rad不会以空终止缓冲区。对于更一般的C用法,它可能需要一个简单的包装器来执行空终止。或者对于打印,只需将赋值更改为putchar()。
下面是一个不受重入性问题或参数大小/类型限制的函数版本:
#define FMT_BUF_SIZE (CHAR_BIT*sizeof(uintmax_t)+1)
char *binary_fmt(uintmax_t x, char buf[static FMT_BUF_SIZE])
{
char *s = buf + FMT_BUF_SIZE;
*--s = 0;
if (!x) *--s = '0';
for (; x; x /= 2) *--s = '0' + x%2;
return s;
}
请注意,这段代码适用于2到10之间的任何底数,只要将2替换为所需的底数。用法是:
char tmp[FMT_BUF_SIZE];
printf("%s\n", binary_fmt(x, tmp));
其中x是任意的积分表达式。
还有一种想法是将数字转换为十六进制格式,然后将每个十六进制密码解码为四个“位”(1和0)。Sprintf可以为我们做位操作:
const char* binary(int n) {
static const char binnums[16][5] = { "0000","0001","0010","0011",
"0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111" };
static const char* hexnums = "0123456789abcdef";
static char inbuffer[16], outbuffer[4*16];
const char *i;
sprintf(inbuffer,"%x",n); // hexadecimal n -> inbuffer
for(i=inbuffer; *i!=0; ++i) { // for each hexadecimal cipher
int d = strchr(hexnums,*i) - hexnums; // store its decimal value to d
char* o = outbuffer+(i-inbuffer)*4; // shift four characters in outbuffer
sprintf(o,"%s",binnums[d]); // place binary value of d there
}
return strchr(outbuffer,'1'); // omit leading zeros
}
puts(binary(42)); // outputs 101010
我优化了顶部的解决方案的大小和c++ -ness,并得到了这个解决方案:
inline std::string format_binary(unsigned int x)
{
static char b[33];
b[32] = '\0';
for (int z = 0; z < 32; z++) {
b[31-z] = ((x>>z) & 0x1) ? '1' : '0';
}
return b;
}