Converting an Int32 into a BitArray

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Everybody,

I wrote a little method that gets me an Int32 from a BitArray. This method
looks like this:

private Int32 GetIntVal( BitArray iArray )
{
Byte[] lByteArr = new Byte[ iArray.Count ];

iArray.CopyTo( lByteArr, 0 );

return BitConverter.ToInt32( lByteArr, 0 );
}

Now I need the according Set method to Set an Int32 value to a BitArray ????

private void SetIntVal( Int32 iValue, ref BitArray iArray )
{
// ??????
}

can anyone help ??

Thanks :-)
 
private Int32 GetIntVal( BitArray iArray )
{
Byte[] lByteArr = new Byte[ iArray.Count ];

iArray.CopyTo( lByteArr, 0 );

return BitConverter.ToInt32( lByteArr, 0 );
}

FYI you can also use an int[] like so

int[] arr = new int[ (iArray.Count + 32) / 32 ];
iArray.CopyTo( arr, 0 );
return arr[0];

Now I need the according Set method to Set an Int32 value to a BitArray ????

private void SetIntVal( Int32 iValue, ref BitArray iArray )
{

iArray = new BitArray(new int[] {iValue});


Mattias
 
You can use the System.BitConverter to achieve this.

I'd probably just return it though rather than passing it by reference. Just
my preference however it's up to you.

private BitArray SetIntVal( Int32 iValue )
{
return new BitArray(System.BitConverter.GetBytes(iValue));
}

Hope this is what you were looking for.

-Eric
 
Back
Top