简体   繁体   English

C中十进制转二进制使用For

[英]Decimal To Binary Conversion in C using For

I am not able to convert from decimal to binary in C.Everytime I get a output which is one less than the desired output.For ex.:5 should be 101 but shows up as 100 or 4 should be 100 but shows up as 99.我无法在 C 中将十进制转换为二进制。每次我得到一个 output,它比所需的 output 少一个。例如:5 应该是 101 但显示为 100 或 4 应该是 100 但显示为 99 .

#include<stdio.h>
#include<math.h>
void main() {
    int a,b=0;
    int n;
    printf("Enter a Decimal Number\n");
    scanf("%d",&n);
    for(int i=0;n>0;i++) {
        a=n%2;
        n=n/2;
        b=b+(pow(10,i)*a);
    }
    printf("%d",b);
}

My output is always one less than the correct answer and I dont know why.It fixes the problem if take b as 1 instead of 0 in the beginning but i dont know why.Please Help.I have just started C a few days ago.我的 output 总是比正确答案少一个,我不知道为什么。如果一开始将 b 设为 1 而不是 0,它可以解决问题,但我不知道为什么。请帮助。我几天前才开始使用 C。

pow is a floating-point function; pow是一个浮点数 function; it takes a double argument and returns a double value.它接受一个double精度参数并返回一个double值。 In the C implementation you are using, pow is badly implemented.在您使用的 C 实现中, pow的实现很糟糕。 It does not always produce a correct result even when the correct result is exactly representable.它并不总是产生正确的结果,即使正确的结果是可以精确表示的。 Stop using it for integer arithmetic.停止将其用于 integer 算术。

Rewrite the code to compute the desired power of ten using integer arithmetic.重写代码,使用 integer 算法计算所需的 10 次方。

Also, do not compute binary numerals by encoding them a decimal within a int type.另外,不要通过在int类型中将二进制数字编码为十进制来计算二进制数字。 It is wasteful and quickly runs into bounds of the type.它很浪费并且很快就会遇到类型的界限。 Use either bits within an unsigned type or an array of char .使用unsigned类型中的位或char数组。 When scanf("%d",&n);scanf("%d",&n); executes, it converts the input string into binary and stores that in n .执行时,它将输入字符串转换为二进制并将其存储在n中。 So n is already binary;所以n已经是二进制的; you do not need to decode it.你不需要解码它。 Use a loop to find its highest set bit.使用循环找到其最高设置位。 Then use another loop to print each bit from that position down to the least significant bit.然后使用另一个循环打印从 position 到最低有效位的每一位。

This code seems fine.这段代码看起来不错。 I quickly tested it on an online compiler and it seems to be working okay.我很快在在线编译器上对其进行了测试,它似乎工作正常。

I am very sure it has to do with different versions of compilers.我非常确定它与不同版本的编译器有关。 compiler which I tested your code in: https://www.onlinegdb.com/online_c_compiler我在其中测试了您的代码的编译器: https://www.onlinegdb.com/online_c_compiler

Edit:编辑:

pow() function is not reliable when used with integers since the integer you pass into it as parameter is implicitly converted into data type of double and returns double as output. When you stuff this value into the integer again, it drops the decimal values. pow() function 在与整数一起使用时不可靠,因为作为参数传递给它的 integer 被隐式转换为 double 数据类型并返回 double 作为 output。当您再次将此值填充到 integer 时,它会丢弃十进制值。 Some compilers seem to produce "correct" result with their version of pow() while some don't.有些编译器似乎用他们的pow()版本产生了“正确”的结果,而有些则没有。

Instead, you can use a different approach to solve your decimal to binary conversion without errors in general use:相反,您可以使用不同的方法来解决您的十进制到二进制的转换,而不会在一般使用中出错:

#include<stdio.h>
void main() {
    int remainder,result = 0,multiplier = 1;
    int input;
    printf("Enter a Decimal Number\n");
    scanf("%d",&input);
    while(input){
        remainder = input%2;
        result = remainder*multiplier + result;
        multiplier*=10;
        input/=2;
    }
    printf("The binary version of the decimal value is: %d",result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM