-1

I have a byte array (byte[16]) with one byte in each cell:

0000000000010010 -> [0][0] ..... [1][0][0][1][0].

How do I get the result with base 2 logic?

I want to get the result : 18. I use C#.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael
  • 31
  • 7
  • Like [this](http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string)? – Brian Nov 22 '13 at 21:43
  • Does your array represent a 16-bit binary number with each byte representing a single bit (0 or 1)? – Alex Nov 22 '13 at 21:45
  • Perhaps something like this? `if (BitConverter.IsLittleEndian) Array.Reverse(bytes); int i = BitConverter.ToInt32(bytes, 0);` – Kemo Sabe Nov 22 '13 at 21:47
  • @KemoSabe Your comment assumes that the byte array is the 4 byte representation of a 32bit integer, which doesn't appear to be the case here - each byte in the array appears to represent a single *bit*. – Iridium Nov 22 '13 at 21:51
  • Depending on how you got that array in the first place, you might want to use the [`BitArray`](http://msdn.microsoft.com/en-us/library/system.collections.bitarray(v=vs.110).aspx) class instead. – Tim S. Nov 22 '13 at 21:52
  • @Iridium agreed, I wasn't sure at first. I very much like your solution. – Kemo Sabe Nov 22 '13 at 21:53

2 Answers2

4

Should be able to use the following, on the assumption that your byte array is purely 0s and 1s), (although if this is the case, a bool[] would probably be a better choice), and that the most significant bit is the 0th element.

private int BytesToInt(byte[] byteArray)
{
    // Start with zero
    int result = 0;
    foreach (var b in byteArray)
    {
        // For each item in the array, first left-shift the result one bit
        result <<= 1;
        // If the byte is non-zero, set the lowest bit in the result
        if (b != 0) result |= 1;
    }
    return result;
}
Iridium
  • 23,323
  • 6
  • 52
  • 74
  • 1
    Could you explain what result <<= 1; and result |= 1; mean? I've never seen those before. – Kemo Sabe Nov 22 '13 at 21:52
  • 3
    `<<=` is the left-shift assignment operator (equivalent here to: `result = result << 1`, and `|=` is the or assignment operator (equivalent to `result = result | 1`). – Iridium Nov 22 '13 at 21:54
  • My math lacks the skills to understand left-shift, but thanks for the explanation. – Kemo Sabe Nov 22 '13 at 21:58
  • 1
    @KemoSabe left-shift by 1 is just the same as multiplying by 2. More generally, left-shift by n is the same as multiplying by 2^n. Similarly, right-shift is division by a power of 2 (and dropping any fractions). – Iridium Nov 22 '13 at 22:01
-1

A little bit-twiddling should do you. A LINQ one-liner:

public static ushort ToUShort( this byte[] buffer )
{
  const int ushort_bits = sizeof(ushort) * 8 ;
  int bits = ushort_bits - 1 ;
  return (ushort) buffer
                  .Take( ushort_bits ) 
                  .Select( b => b != 0 ? 1 : 0 )
                  .Aggregate(0 , (acc,b) => acc | (b<<(bits--)))
                  ;
}

or the equally succinct (and probably faster):

public static ushort ToUShort( this byte[] buffer )
{
  uint acc = 0 ;
  int bits = sizeof(ushort) * 8 - 1 ;
  int max  = sizeof(ushort) * 8     ;
  for ( int i = 0 ; i < max  ; ++i )
  {
    acc |= (buffer[i]==0?0u:1u)<<(bits--) ;
  }
  return (ushort) acc ;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135