在C中将十进制转换为二进制

我试图将十进制转换为二进制,如192到11000000.我只需要一些简单的代码来做到这一点,但到目前为止我的代码不起作用:

void dectobin(int value, char* output) { int i; output[5] = '\0'; for (i = 4; i >= 0; --i, value >>= 1) { output[i] = (value & 1) + '0'; } } 

任何帮助将非常感激!

该值不是小数。 计算机内存中的所有值都是二进制的。

您要做的是使用特定的基础将int转换为字符串。 有一个function,它被称为itoahttp://www.cplusplus.com/reference/cstdlib/itoa/

首先, 192不能用4位表示

192 = 1100 0000 ,最少需要8位。

这是一个简单的C程序,用于将十进制数字系统转换为二进制数字系统

 #include  #include  int main() { long decimal, tempDecimal; char binary[65]; int index = 0; /* * Reads decimal number from user */ printf("Enter any decimal value : "); scanf("%ld", &decimal); /* Copies decimal value to temp variable */ tempDecimal = decimal; while(tempDecimal!=0) { /* Finds decimal%2 and adds to the binary value */ binary[index] = (tempDecimal % 2) + '0'; tempDecimal /= 2; index++; } binary[index] = '\0'; /* Reverse the binary value found */ strrev(binary); printf("\nDecimal value = %ld\n", decimal); printf("Binary value of decimal = %s", binary); return 0; } 

5个数字不足以满足您的示例(192)。 可能你应该增加output

几天前,我正在寻找快速便携sprintf("%d", num) 。 通过GCC在itoa页面找到了这个实现:

 /** * C++ version 0.4 char* style "itoa": * Written by Lukás Chmela * Released under GPLv3. */ char* itoa(int value, char* result, int base) { // check that the base if valid if (base < 2 || base > 36) { *result = '\0'; return result; } char* ptr = result, *ptr1 = result, tmp_char; int tmp_value; do { tmp_value = value; value /= base; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while ( value ); // Apply negative sign if (tmp_value < 0) *ptr++ = '-'; *ptr-- = '\0'; while(ptr1 < ptr) { tmp_char = *ptr; *ptr--= *ptr1; *ptr1++ = tmp_char; } return result; } 

这是将十进制数转换为二进制数的算法

  • 将输入的十进制数除以2并存储余数。
  • 将商存储回输入数字变量。
  • 重复此过程直到商变为零。
  • 等价二进制数将是上述过程中的相反顺序的余数。

你可以在这里检查c程序http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html

它看起来像这样,但要小心,你必须扭转结果字符串:-)

 #include  #include  #include  char output[256]=""; int main() { int x= 192; int n; n = x; int r; do { r = n % 2; if (r == 1) strcat(output,"1"); else strcat(output,"0"); n = n / 2; } while (n > 0); printf("%s\n",output); } 

所以…你检查了代码的输出,以了解它为什么不起作用?

 So iteration 1 of your loop: value = 192 i = 4 output[i] = (11000000 & 1) + '0' = 0 + 48 = 48 (char `0`) Iteration 2 of your loop: value = 96 i = 3 output[i] = (1100000 & 1) + '0' = 0 + 48 = 48 (char `0`) Iteration 3 of your loop: value = 48 i = 2 output[i] = (110000 & 1) + '0' = 0 + 48 = 48 (char `0`) Iteration 4 of your loop: value = 24 i = 1 output[i] = (11000 & 1) + '0' = 0 + 48 = 48 (char `0`) Iteration 5 of your loop: value = 12 i = 0 output[i] = (1100 & 1) + '0' = 0 + 48 = 48 (char `0`) Final string: "00000" and you wanted: "11000000" 

看到你的代码有什么问题? 不。 我也没有走得太远。 将输出/循环更改为:

 output[8] = '\0'; for (i = 7; i >= 0; --i, value >>= 1) 

然后你会得到正确的结果。

我建议只是一种更通用的方法,你使用的是一个固定长度的字符串,它将你限制为一个certian长度的二进制数。 您可能想要执行以下操作:

 loop while number dividing down is > 0 count number of times we loop malloc an array the correct length and be returned 
 #include  #include  void bin(int num) { int n = num; char *s = malloc(sizeof(int) * 8); int i, c = 0; printf("%d\n", num); for (i = sizeof(int) * 8 - 1; i >= 0; i--) { n = num >> i; *(s + c) = (n & 1) ? '1' : '0'; c++; } *(s + c) = NULL; printf("%s", s); // or you can also return the string s and then free it whenever needed } int main(int argc, char *argv[]) { bin(atoi(argv[1])); return EXIT_SUCCESS; } 

你也可以在函数下使用while循环来完成它。 我只是在寻找我的解决方案,但我得到的解决方案并不合适,所以我相应地采用了实际方法(除了使用2直到得到0并将提醒存储在一个数组中)并打印出数组的反向和共享这里

 #include  int main() { long long int a,c; int i=0,count=0; char bol[10000]; scanf("%lld", &a); c = a; while(a!=0) { bol[i] = a%2; a = a / 2; count++; i++; } if(c==0) { printf("0"); } else { for(i=count-1; i>=0; i--) { printf("%d", bol[i]); } } printf("\n"); return 0; } 

// C使用Stack将Decimal转换为二进制的程序

 #include #define max 100 int stack[max],top=-1,i,x; void push (int x) { ++top; stack [top] = x; } int pop () { return stack[top]; } void main() { int num, total = 0,item; print f( "Please enter a decimal: "); scanf("%d",&num); while(num > 0) { total = num % 2; push(total); num /= 2; } for(i=top;top>-1;top--) { item = pop (); print f("%d",item); } } 

也许了解该算法可以让您编写或修改自己的代码以满足您的需要。 我确实看到你没有足够的char数组长度来显示192的二进制值(你需要8位二进制数,但你的代码只给出5位二进制数字)

这是一个清楚解释算法的页面 。

我不是C / C ++程序员,所以这里是基于算法示例的C#代码贡献。

 int I = 0; int Q = 95; string B = ""; while (Q != 0) { Debug.Print(I.ToString()); B += (Q%2); Q = Q/2; Debug.Print(Q.ToString()); I++; } Debug.Print(B); 

所有Debug.Print只是为了显示输出。

 //decimal to binary converter long int dec2bin(unsigned int decimal_number){ if (decimal_number == 0) return 0; else return ((decimal_number%2) + 10 * dec2bin(decimal_number/2)); } 

C语言将十进制转换为二进制

 #include void main() { long int n,n1,m=1,rem,ans=0; printf("\nEnter Your Decimal No (between 0 to 1023) :: "); scanf("%ld",&n); n1=n; while(n>0) { rem=n%2; ans=(rem*m)+ans; n=n/2; m=m*10; } printf("\nYour Decimal No is :: %ld",n1); printf("\nConvert into Binary No is :: %ld",ans); } 
 int main() { int n, c, k; printf("Enter an integer in decimal number system: "); scanf("%d", &n); printf("%d in binary number system is: ", n); for (c = n; c > 0; c = c/2) { k = c % 2;//To k = (k > 0) ? printf("1") : printf("0"); } getch(); return 0; }