C:转换int数组重一些presenting位成一个单一的诠释? [英] C: Convert int array representing bits of a number into a single Int?

查看:118
本文介绍了C:转换int数组重一些presenting位成一个单一的诠释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C初学者,想知道如何转换的重新present数字的位数为它重新presents的整数的数组。

I am a beginner in C and wish to know how to convert an array of numbers that represent the bits of a number into the integer it represents.

我在网上搜索,但只有发现字符数组为int。

I searched online but only found char array to int.

我正在转换器和需要int数组的位值切换从1到0,反之亦然(即二进制)。到目前为止,我已成功地使整数数组进行切换,但我无法弄清楚如何为数字数组转换为单数的阵列重新presents。我需要这个整数,所以我可以将其转换成十进制数,用户可以查看。

I am making converter and need to int array to toggle the bit values from 1 to 0 and vice versa(ie binary). So far i have managed to make the array integers toggle, but I cannot figure out how to change the array of numbers into the single number the array represents. I need this integer so i can convert it into a decimal number which the user can view.

有没有什么简单的解决这个?

Is there any simple solution to this?

斌=整型变量存储二进制数

bin= integer variable to store the binary number

arrbin =整数数组存储位的状态,即(00000000)

arrbin = integer array to store the state of the bit ie(00000000)

            for (i = 0; i < 8; ++i )
                {
                bin = bin + (10 * arrbin[i]);
                }

编辑:我想我现在看到的,而不是价值的问题,正在实施的10家,这些数字只乘以每10指数将尝试,看看这个修复它。

I think i see the problem now, instead of the values being implemented every exponent of 10 up, the numbers were only being multiplied by 10. will try and see if this fixes it.

编辑#2:我不意味二进制转换为十进制。我的意思是数字即{1,0,0,0,0,0,0,1)的数组转换为整数(整数varible = 10000001不是129)

EDIT #2: I dont mean conversion of binary to decimal. I meant conversion of array of numbers ie{1,0,0,0,0,0,0,1) to integer (integer varible = 10000001 not 129)

推荐答案

您basiclly有答案,但你会从另一个方向更好地做,即:

You basiclly have the answer but you would be better doing it from the other direction ie:

int multiplier = 1;
for (i = 7; i >= 0; --i )
{
    bin += (multiplier * arrbin[i]);
    multiplier *= 10;
}

这样你可以改变你使用轻松数组的长度。

that way you can change the length of the array you are using easily.

这篇关于C:转换int数组重一些presenting位成一个单一的诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆