简体   繁体   English

C将4位数字转换为二进制

[英]C Converting 4 digit numbers to binary

I want to print the binary equivalent of the numbers from a file in a 20 bit field with spaces after every 4 bits but when the number exceeds 1024 (I've tested this), it no longer outputs in binary but in a base I don't know (ie 1234 = 0000 0014 1006 5408) 我想在20位字段中每4位后用空格打印文件中数字的二进制等效项,但是当数字超过1024(我已经测试过)时,它不再以二进制形式输出,而是以基数输出不知道(例如1234 = 0000 0014 1006 5408)

Here is my code 这是我的代码

#include <stdio.h>
long convertToBinary(long);

int main()
{
    char binNum[20]; //binary number char array
    int i;
    long b, decNum; //b is output binary number, decNum is each input number from file
    FILE *filePtr;
    filePtr = fopen("numbers.txt", "r");
    while (!feof(filePtr))
    {
        fscanf(filePtr, "%li", &decNum);
        if (decNum < 0  || decNum > 65535)
        {
            printf("Error, %5li is out of range.\n", decNum);
            continue;
        }
        b = convertToBinary(decNum); //function call for binary conversion

        i = 18;
        while (i >= 0)
        {
            if (i == 4 || i == 9 || i == 14)
            {
                binNum[i] = ' ';
            }
            else if (b != 0)
            {
                binNum[i] = b % 10 + '0';
                b /= 10;
            }
            else 
            {
                binNum[i] = '0';
            }
            i--;
        }
        binNum[19] = '.';
        printf("The binary representation of %5li is: ", decNum);
        for (i = 0; i<20; i++)
        {
            printf("%c", binNum[i]);
        }
        printf("\n");
    }

return 0;
}

//Recursion function
long convertToBinary(long number)
{
    if (number == 0)
    {
        return 0;
    }
    else
    {
        return (number % 2 + 10 * convertToBinary(number / 2));
    }
}

The file numbers.txt has numbers like : 0 1 2 3 16 17 1234 5678 65535 文件numbers.txt数字如下:0 1 2 3 16 17 1234 5678 65535

Your convertToBinary function is producing a decimal-coded-binary (as opposed to a binary-coded-decimal) which uses a decimal digit per bit. 您的convertToBinary函数正在生成一个十进制编码的二进制(与二进制编码的十进制相对),该位每位使用一个十进制数字。

It looks like long has the same range as int on your system, ie 2 31 , which covers ten decimal digits. 看起来long与系统上的int具有相同的范围,即2 31 ,它覆盖十个十进制数字。 Once you get eleventh digit, you get an overflow. 一旦获得第十一位,就会溢出。

Switching to unsigned long long , which has 64 bits, will fix the problem. 切换到具有64位的unsigned long long可以解决此问题。 However, this is sub-optimal: you would be better off writing a to-binary conversion that writes zeros and ones into an array of char s that you provide. 但是,这是次优的:您最好编写一个将二进制数零和一写入您提供的char数组的二进制转换。 You can do it recursively if you'd like, but an iterative solution will do just fine. 您可以根据需要递归执行此操作,但是迭代解决方案就可以了。

Note that the to-binary conversion does not need to insert spaces into char array. 请注意,二进制转换不需要在char数组中插入空格。 This can be done by your main function. 这可以通过您的main功能来完成。

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

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