I have an integer array of length 900 that contains only binary data [0,1]. I want to short the length of the array without losing binary data formate(original array values).
Is it possible to short the length of array of 900 into 10 or 20 length in C#???

Chen PengPosted Feb 11, 2011, 9:10 AM
If so, the answer is definitely yes. You can follow this way:
BinaryDataArray Int32
0,1 1*2+0=2
1,0,1 1*4+0*2+1=5
0,1,0,1,1 1*16+1*8+0*4+1*2+0=26
I wrote a function to Store as below:
Wish help :)
public static uint StoreBit(int[] bitArray)
{
// ensure bitArray is not null
if (bitArray == null)
{
throw new Exception("bitArray should not be null");
}
// ensure Length of bitArray is less than 32, otherwise we cannot store it in UInt32
if (bitArray.Length > 32)
{
throw new Exception();
}
//Store in one UInt varaible
UInt32 result = 0;
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i] == 1)
{
result += ((uint)bitArray[i]) << i;
}
else if (bitArray[i] == 0)
{
// do nothing
}
else
{
throw new Exception("we only accept bit");
}
}
return result;
}
Guest UserPosted Feb 7, 2011, 8:52 AM