Reputation: 91
I have an array as such:
int[] fourbits = new int[4];
fourbits[0] = neuroninputs.input1;
fourbits[1] = neuroninputs.input2;
fourbits[2] = neuroninputs.input3;
fourbits[3] = neuroninputs.input4;
Each element contains a binary value. For example :
Console.WriteLine(fourbits[0]);
outputs 1.
What I am trying to do is to take every value from this array(1010) and convert this to decimal and print this value(10).
Upvotes: 4
Views: 377
Reputation: 101681
First concatenate all the bits into a string
using String.Join
,then use Convert.ToInt32
method specifying the base
parameter.:
var value = Convert.ToInt32(string.Join("", fourbits), 2);
Console.WriteLine(value);
Note: You need to make sure that your array contains only ones
and zeros
in order to speficy base parameter as 2
. Otherwise you will get a FormatException
.
Upvotes: 3