【问题标题】:How can I convert a decimal number into binary, saving binary digits in array?如何将十进制数字转换为二进制,将二进制数字保存在数组中?
【发布时间】:2019-10-29 17:44:55
【问题描述】:

我需要将十进制转换为二进制,将每个数字保存在一个大小为 10 的数组中。

实际上,我可以简单地将十进制更改为二进制,但我对它的数字有困难。

假设int a = 3,那么它的二进制形式是11

但我需要将它保存在大小为10 的一维数组中,以打印为0000000011,而不仅仅是11

再举一个例子,如果我输入十进制87,我的代码应该打印01010111

如何计算每个数字并将它们保存到数组中的正确位置?

【问题讨论】:

  • 你能解释一下为什么需要一个大小为 10 的数组吗?位以 2 的幂存储。4 位、8 位、16 位、32 位等。10 位不允许存储 16 位整数允许的最大值,并且至少有 2 位为空,对于 8 位整数。另外,您是否期望有符号整数?如果是这样,有一点保留来存储标志。它是小端架构上最左边的一个,而大端架构上最右边。在解决您的问题之前,有很多问题需要回答。
  • 关于:01010111. 这是0x67,而不是0xB7 这种错误可能是造成您很多困惑的根本原因。也许您的意思是:10110111.

标签: c binary


【解决方案1】:

我现在无法对其进行测试,但这可能会有所帮助:

// Here there should be the necessary include to enable printing

unsigned int size = 10; // The size of the array that should store the bin output
int          a    = 3;  // The int to be converted to bin

// A function that returns a pointer to a byte, that will be interpreted as an array
unsigned short int
*IntToBin (int input)
{
    static unsigned short int output[size]; // Declaring the array to be returned by
                                            // the function, with keyword 'static' to
                                            // allow it to be referenced from outside
    for (int i = 0; i < size; i++) {
        output[i] = (input >> (size - 1 - i)) & 0x1; // Populating output array by
                                                     // bit-shifting the input int
                                                     // and doing a bitwise 'and' to
                                                     // keep only least significant
                                                     // digit
    }
    return output; // Returning the output array (an array is essentially a pointer,
                   // so this matches the function declaration)
}

// The main function, that will take care of calling the conversion function and
// printing its output
void
main ()
{
    unsigned short int result[] = IntToBin (a); // Assigns the output of the conversion
                                           // function to an array
    // Prints the input and output of the conversion function
    print("a = ");
    print(a);
    print("\na in bin = ");
    for (int i = 0; i < size; i++) {
        print (result[i]);
    }
    print("\n");
}

但是,正如 cmets 中所述,有符号整数的行为与无符号整数的行为不同。你的输出数组的长度为10 有点奇怪。

https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

【讨论】:

  • 您正在返回一个指向堆栈分配对象的指针,这将产生意想不到的结果。 c中也没有数据类型字节。
  • 我已经编辑了我的帖子,我相信我已经解决了你的所有建议。
【解决方案2】:

查看数字n 的位的技巧是按位并用 1 获得最低有效位,然后右移一位,然后再按位和 1 再次获得第二个位置的位,然后以此类推。

n>>1 & 0x1

现在要将它存储在一个数组中,您应该注意到最低有效位应该放在数组的最后一个元素中,所以如果数组a 的大小为 10。最低有效位应该在 a[ 9]

将这两者结合起来,您将数组索引从最后一个位置计数为零,然后移动您的数字以获取该位置的位。现在您存储在数组中的不是 0 和 1,而是字符“0”和字符“1”。您可以通过将数字 0 添加到 '0' 将数字 0 转换为字符 0,并将数字 1 通过添加到 '0' 来转换。

结合以上所有想法,您将得到以下代码。

void int2bin(int n, char* b, int sz) {
    for (int p=sz-1; p>=0; p--,n>>=1) {
        b[p] = '0' + (n & 0x1);
    }   
}

int main(int argc, char**argv){
    char a[10]={'0'};
    int2bin(atoi(argv[1]),a,sizeof(a));

    for (int i=0;i<sizeof(a);++i)
        printf("%c",a[i]);

    return 0;
}

请记住,数组大小为 10 时,您可以用二进制表示的最大数为 2^10 - 1 = 1023。如果您还考虑负数,则 2^9 -1 =511 中的最大值和最小值-512

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2011-02-18
    • 2011-01-08
    • 2010-12-08
    • 2013-11-24
    • 2019-08-12
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多