我可以用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"
当前回答
使用以下功能:
void conbin(int num){
if(num != 0)
{
conbin(num >> 1);
if (num & 1){
printf("1");
}
else{
printf("0");
}
}
}
其他回答
是否有一个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()。
/* Convert an int to it's binary representation */
char *int2bin(int num, int pad)
{
char *str = malloc(sizeof(char) * (pad+1));
if (str) {
str[pad]='\0';
while (--pad>=0) {
str[pad] = num & 1 ? '1' : '0';
num >>= 1;
}
} else {
return "";
}
return str;
}
/* example usage */
printf("The number 5 in binary is %s", int2bin(5, 4));
/* "The number 5 in binary is 0101" */
我喜欢的代码由paniq,静态缓冲区是一个好主意。但是,如果你想在一个printf()中有多个二进制格式,它就失败了,因为它总是返回相同的指针并覆盖数组。
下面是一个C风格的下拉列表,它可以在分割缓冲区上旋转指针。
char *
format_binary(unsigned int x)
{
#define MAXLEN 8 // width of output format
#define MAXCNT 4 // count per printf statement
static char fmtbuf[(MAXLEN+1)*MAXCNT];
static int count = 0;
char *b;
count = count % MAXCNT + 1;
b = &fmtbuf[(MAXLEN+1)*count];
b[MAXLEN] = '\0';
for (int z = 0; z < MAXLEN; z++) { b[MAXLEN-1-z] = ((x>>z) & 0x1) ? '1' : '0'; }
return b;
}
这里有一个快速的技巧来演示如何做你想做的事情。
#include <stdio.h> /* printf */
#include <string.h> /* strcat */
#include <stdlib.h> /* strtol */
const char *byte_to_binary
(
int x
)
{
static char b[9];
b[0] = '\0';
int z;
for (z = 128; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
int main
(
void
)
{
{
/* binary string to int */
char *tmp;
char *b = "0101";
printf("%d\n", strtol(b, &tmp, 2));
}
{
/* byte to binary string */
printf("%s\n", byte_to_binary(5));
}
return 0;
}
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