5

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.

I searched online but only found char array to int.

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 = integer array to store the state of the bit ie(00000000)

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

EDIT: 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.

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)

user3682308
  • 53
  • 1
  • 1
  • 6
  • 2
    Is there any sample input and output? – timrau May 28 '14 at 23:31
  • 1
    Also please post your code, even if it doesn't work. Shows us what you've already tried. – AntonH May 28 '14 at 23:34
  • For future visitors: storing 0x81 as 10000001 is an interesting task, but it is also useless for any real-world use. You can't do math on it, it can't store even a `short int` (16-bit minimum) value, all you can do is convert it to a string, and converting directly from `arrbin` to string is far easier than going to this decimal-coded-binary representation. – Ben Voigt Apr 08 '15 at 04:44

4 Answers4

7

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Oh okay I now understand. I only looked at the top answer thinking it was the correct one. Although I did find the 1st answer on the duplicate much easier to understand. Now i realize this is also the correct answer. My apologizes – user3682308 May 29 '14 at 02:06
  • @user3682308 can you mark the answer as correct if it has answered your question. – Fantastic Mr Fox May 29 '14 at 22:31
5

How about do it manually, the same way you do it to convert binary to int using powers of 2

#include <stdio.h>
#include <string.h>

main()
{
   int arr[4]={1,0,0,1};
   int i;
   int output=0, power=1;
   for (i=0; i<4; i++)
   {

       output += arr[3-i]*power;
       power *= 2;
   }

   printf("%d\n", output);

in this example

arr = 1 0 0 1
output = 1*2^0 + 0*2^1 + 0*2^2 + 1*2^3 = 9
chouaib
  • 2,763
  • 5
  • 20
  • 35
2

You're forgetting that binary doesn't work by multiplying the value by 10, but by 2 (multiplying by 10 would be good it you're converting a decimal number to an int, but there are already functions for that).

So if I take your code and apply the appropriate multiplication:

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

Instead of multiplying by 2 the value of bin, you can use bitshifts instead (replace bin *= 2; with bin = bin << 1;).

All this supposes that the arrbin array contains valid values, and not, for example, ASCII characters of the values. If this is the case, you will have to convert it to a valld value before adding it to bin.

AntonH
  • 6,359
  • 2
  • 30
  • 40
0

I had to use this method to convert my binary array to a decimal integer. These other solutions didn't work for me, perhaps my array wasn't setup the same but either way, here is my answer to this question.

int bin_to_int_digit(int* in, int length)
{
    int exp = 0;
    int i;
    int bin = 0;
    for (i = length - 1; i >= 0; i--)
    {
        bin += in[i] * pow(2, exp);
        exp += 1;
    }
    return bin;
}

This subroutine requires the use of the math.h header so make sure you include -lm flag when compiling with gcc or else you will get an error.

Zach Pedigo
  • 396
  • 2
  • 9