Reputation: 6595
I have an array of 8 bit with the following representation. Data[i] = 192=11000000
, data[i+1]= 85=01010101
, i need to transform the represenatation into 16bit array where newArr[i] =343=101010111
. The same numbers only shifted to right. My first idea was to use the field i have bpp (number of bits =10). So var t=16-10
. Data[i] >>t
. And Data[i+1]>>t
however this doesn't give the correct answer. Please help
Upvotes: 0
Views: 1550
Reputation: 6595
my final code :D
public override short[] GetShortDataAlignedRight()
{
short[] ReturnArray = new short[_channels[0].Data.Length / 2];
if (_channels[0].Bpp == 8)
{
Buffer.BlockCopy(_channels[0].Data, 0, ReturnArray, 0, _channels[0].Data.Length);
}
else
{
short tempData;
int offsetHigh = 8 - (16 - _channels[0].Bpp);
int offsetLow = (16 - _channels[0].Bpp);
for (int i = 0, j = 0; i < _channels[0].Data.Length; i += 2, j++)
{
tempData = (short)(_channels[0].Data[i] >> offsetLow);
tempData |= (short)(_channels[0].Data[i + 1] << offsetHigh);
ReturnArray[j] = tempData;
}
}
return ReturnArray;
}
Upvotes: 0
Reputation: 378
I take your question to mean you want to do this conversion: combine aaaaaaaa
and bbbbbbbb
into bbbbbbbbaaaaaaaa
and then apply a right-shift by 6, yielding 000000bbbbbbbbaa
.
Then this will be your code:
using System;
public class Test
{
public static void Main()
{
byte[] src = new byte[2] { 192, 85 };
ushort[] tgt = new ushort[1];
for ( int i = 0 ; i < src.Length ; i+=2 )
{
tgt[i] = (ushort)( ( src[i+1]<<8 | src[i] )>>6 );
}
System.Console.WriteLine( tgt[0].ToString() );
}
}
If what you want to do is combine to aaaabbbbbbbbaa
, then this will require |
-ing a src[i]<<10
in a second step, as there is no circular shift operator in C#.
Upvotes: 1
Reputation: 12934
I have these two functions for you, to shift in a byte-array:
static public void ShiftLeft(this byte[] data, int count, bool rol)
{
if ((count <0) || (count > 8))
throw new ArgumentException("Count must between 0 and 8.");
byte mask = (byte)(0xFF << (8 - count));
int bits = rol ? data[0] & mask : 0;
for (int i = data.Length - 1; i >= 0; i--)
{
int b = data[i] & mask;
data[i] = (byte)((data[i] << count) | (bits >> (8 - count)));
bits = b;
}
}
static public void ShiftRight(this byte[] data, int count, bool rol)
{
if ((count <0) || (count > 8))
throw new ArgumentException("Count must between 0 and 8.");
byte mask = (byte)(0xFF >> (7 - count));
int bits = rol ? data[data.Length - 1] & mask : 0;
for (int i = 0; i < data.Length; i++)
{
int b = data[i] & mask;
data[i] = (byte)((data[i] >> count) | (bits << (8 - count)));
bits = b;
}
}
For your code example, call them like this:
byte[] data = ...;
ShiftLeft(data, 2, false);
Assuming you want to copy 2 bytes into 1 ushort, you can use this code (make sure it is even in length!):
byte[] data = ...;
short[] sdata = new short[data.Length / 2];
Buffer.BlockCopy(data, 0, sdata, 0, dataLen);
If you want to copy 1 byte into 1 ushort, then this would be the answer:
byte[] data = ...;
short[] sdata = Array.ConvertAll(data, b => (short)b);
Upvotes: 2