问题描述
我想一个十进制数转换为二进制,但我不知何故最终得到随机ASCII符号作为输出。下面是我的程序:
的#include<&stdio.h中GT;诠释的main()
{
INT数= 0;
INT I = 50;
烧焦二进制[10];
的printf(请输入一个数字:);
scanf函数(%d个,&安培;号码);
而(数字!= 0){
二元[I] =编号%2;
一世 - ;
数=数/ 2;
}
的printf(\\ N%的,二进制); 返回0;
}
有您的code多的问题(没有特定的顺序,但一个我发现他们):
-
您正在初始化
字符
与INT
在行二进制[我] =编号%2
;你应适当变换整数一个字符,像这样的:二元[I] ='0'+(编号%2);
-
您正在写
的printf(\\ n%S,二进制);
,其中二进制
是一个的char [10]
,因此不一定空值终止。你应该多一个字符添加到二进制
并初始化它\\ 0
:二进制字符[11];
二进制[10] ='\\ 0'; -
您错误地初始化您的数组:您开始使用
I = 50
和二进制[10]
。你应该添加一个常数,并使用它两次:的#define MAX_SIZE 32
INT I = MAX_SIZE - 1;
焦炭二进制[MAX_SIZE + 1];
二进制[MAX_SIZE] ='\\ 0';请注意的是避免了您的字符串结束后写的,而
+1 $ C $
1
你忘了, C>这基本上是最后一个问题 -
您正在阅读的
INT
并试图获得其二进制重新presentation;但(真)的二进制重新$ P $负值的psentation通常不是你所期望的。 preFER读取unsigned int类型
:无符号整型数;
scanf函数(%U,&安培;号码); -
您可以取代
号%2
与数字功放&; 1
和数/ = 2
与若干>> = 1
;这是更重presentative你在号上运行什么(二)操作
。此外,这实际上应该正确的负整数,你的版本应该不会(未经测试,反正你平时没有理由希望得到一个负整数的二进制重新presentation)的工作:二进制[我 - ] ='0'+(数字放大器和1);
数字>> = 1; -
您停止后您的号码是零,但是这意味着你没有必要填写
二进制
阵列;这意味着你可以在二进制
(VC ++的调试模式这应该IIRC显示为是
字符)。你应该添加,你的循环后,一些像这样的,从而填补了数组:而(I> = 0)
二进制[我 - ] ='0';
希望帮助!
I'm trying to convert a decimal number to binary but I somehow end up getting 'random' ASCII symbols as an output. Here is my program:
#include <stdio.h>
int main()
{
int number = 0;
int i = 50;
char binary[10];
printf("Enter a number: ");
scanf("%d", &number);
while(number!=0){
binary[i] = number % 2;
i--;
number = number / 2;
}
printf("\n%s", binary);
return 0;
}
There are multiple problems with your code (no specific order but the one I found them):
You are initializing a
char
with anint
at linebinary[i] = number % 2
; you should properly transform the integer to a character, like this:binary[i] = '0' + (number % 2);
You are writing
printf("\n%s", binary);
wherebinary
is achar[10]
, thus not necessarily null-terminated. You should add one more character tobinary
and initialize it to\0
:char binary[11]; binary[10] = '\0';
You are wrongly initializing your array: you start with
i = 50
andbinary[10]
. You should add a constant and use it twice:#define MAX_SIZE 32 int i = MAX_SIZE - 1; char binary[MAX_SIZE + 1]; binary[MAX_SIZE] = '\0';
Note the
-1
you forgot, that avoids that you write after the end of the string, and the+1
that is essentially the last problemYou are reading an
int
and trying to get its binary representation; but the (true) binary representation of a negative value is usually not what you would expect. Prefer reading anunsigned int
:unsigned int number; scanf("%u", &number);
You could replace
number % 2
withnumber & 1
andnumber /= 2
withnumber >>= 1
; which is more representative of exactly what (binary) operations you are running onnumber
. Besides, this should actually work correctly on negative integers, where your version should not (untested, anyway you usually have no reason of wanting to get the binary representation of a negative integer):binary[i--] = '0' + (number & 1); number >>= 1;
You stop after your number is zero, but that means you did not necessarily fill the
binary
array; this implies you may have uninitialized characters at the beginning ofbinary
(on VC++ in debug mode this should IIRC be displayed asÿ
characters). You should add, after your loop, something such as this so as to fill the array:while (i >= 0) binary[i--] = '0';
Hope that helps!
这篇关于用C转换十进制数为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!