0

Possible Duplicate:
How i can convert BitArray to single int?

How can I read an integer to a BitArray(6) (assuming it can be contained) and how to convert a BitArray(6) to unsigned/signed integer.

Community
  • 1
  • 1
blitzkriegz
  • 9,258
  • 18
  • 60
  • 71

2 Answers2

1
byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
0
BitArray FromInt32(Int32 a)
{
    byte[] bytes = BitConverter.GetBytes(a);
    return new BitArray(bytes);
}

For reverse operaton see this question as mentioned before.

Community
  • 1
  • 1
Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110