我可以用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"
当前回答
我的解决方案返回一个int,然后可以在printf中使用。它还可以以大端序或小端序返回位。
#include <stdio.h>
#include <stdint.h>
int binary(uint8_t i,int bigEndian)
{
int j=0,m = bigEndian ? 1 : 10000000;
while (i)
{
j+=m*(i%2);
if (bigEndian) m*=10; else m/=10;
i >>= 1;
}
return j;
}
int main()
{
char buf[]="ABCDEF";
printf("\nbig endian = ");
for (int i=0; i<5; i++) printf("%08d ",binary(buf[i],1));
printf("\nwee endian = ");
for (int i=0; i<5; i++) printf("%08d ",binary(buf[i],0));
getchar();
return 0;
}
输出
big endian = 01000001 01000010 01000011 01000100 01000101 01000110
wee endian = 10000010 01000010 11000010 00100010 10100010 01100010
其他回答
你可以使用一个小表格来提高速度。类似的技术在嵌入式世界中也很有用,例如,反转一个字节:
const char *bit_rep[16] = {
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
};
void print_byte(uint8_t byte)
{
printf("%s%s", bit_rep[byte >> 4], bit_rep[byte & 0x0F]);
}
1我主要指的是嵌入式应用程序,其中优化器不是那么激进,速度差异是可见的。
快速简单的解决方法:
void printbits(my_integer_type x)
{
for(int i=sizeof(x)<<3; i; i--)
putchar('0'+((x>>(i-1))&1));
}
适用于任何大小类型以及有符号整型和无符号整型。'&1'需要处理有符号整型,因为移位可能会进行符号扩展。
有很多方法可以做到这一点。这里有一个超级简单的方法,用于从有符号或无符号32位类型中打印32位或n位(如果有符号,则不输入负号,只打印实际的位),并且不返回回车符。注意,i在移位前递减:
#define printbits_n(x,n) for (int i=n;i;i--,putchar('0'|(x>>i)&1))
#define printbits_32(x) printbits_n(x,32)
如果返回一个包含稍后存储或打印的比特的字符串呢?你可以分配内存并返回它,用户必须释放它,或者你返回一个静态字符串,但如果它再次被调用,或者被另一个线程调用,它会被破坏。两种方法显示:
char *int_to_bitstring_alloc(int x, int count)
{
count = count<1 ? sizeof(x)*8 : count;
char *pstr = malloc(count+1);
for(int i = 0; i<count; i++)
pstr[i] = '0' | ((x>>(count-1-i))&1);
pstr[count]=0;
return pstr;
}
#define BITSIZEOF(x) (sizeof(x)*8)
char *int_to_bitstring_static(int x, int count)
{
static char bitbuf[BITSIZEOF(x)+1];
count = (count<1 || count>BITSIZEOF(x)) ? BITSIZEOF(x) : count;
for(int i = 0; i<count; i++)
bitbuf[i] = '0' | ((x>>(count-1-i))&1);
bitbuf[count]=0;
return bitbuf;
}
电话:
// memory allocated string returned which needs to be freed
char *pstr = int_to_bitstring_alloc(0x97e50ae6, 17);
printf("bits = 0b%s\n", pstr);
free(pstr);
// no free needed but you need to copy the string to save it somewhere else
char *pstr2 = int_to_bitstring_static(0x97e50ae6, 17);
printf("bits = 0b%s\n", pstr2);
一些运行时支持“%b”,尽管这不是标准。
也可以在这里看到一个有趣的讨论:
http://bytes.com/forum/thread591027.html
HTH
以下是我对这个问题的看法。
与大多数其他例子相比的优点:
使用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);
}
还有一种想法是将数字转换为十六进制格式,然后将每个十六进制密码解码为四个“位”(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