Convert byte array to ushort array in C# .Net Microframework
Asked Answered
D

2

6

I need to convert a byte array to a UInt16 (ushort) array. I am able to do so from a byte array to a UInt32 array.

I already looked at this SO question. But I cannot use BitConverter or the solution given in the referenced question.

I also referred to these questions as well: THIS and THIS.

This is what I have tried so far.

for (uint objIndex = 0; objIndex < data.Length; ++objIndex)
{
   data[objIndex] = (Convert.ToUInt16(byteArray[objIndex * sizeof(UInt16) + 0].ToString()) << 8) 
                               + byteArray[objIndex * sizeof(UInt16) + 1]; 
   // Error - Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists.

   data[objIndex] = ((ushort)(byteArray[objIndex * sizeof(UInt16) + 0]) << 8)
                               + byteArray[objIndex + 1]; 
   // Error - Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists.
}

Please let me know what is missing here.

EDIT: I was able to fix it after casting each of the number to an ushort as shown below.

for (ushort objIndex = 0; objIndex < data.Length; ++objIndex)
{
     ushort length = sizeof(UInt16);
     data[objIndex] = (ushort)( (ushort)(byteArray[objIndex * length] << (ushort)8) +
                                                 byteArray[objIndex + 1] );
}
Dierdredieresis answered 26/12, 2013 at 16:39 Comment(3)
Did you try the casts suggested by the compiler? ie: data[objIndex] = (ushort) (((ushort)(byteArray[objIndex * sizeof(UInt16) + 0]) << 8) + byteArray[objIndex + 1]; )Favus
Instead of editing your question, please post the solution as an aswer.Cleanshaven
Yes, I tried it and did not work. But I was able to make the compiler error go away. Now I have to test if it works.Dierdredieresis
D
1

Finally it turned out to be a very simple fix (feeling silly right now). I was able to make the compiler error go away after casting each of the numbers I use to an ushort as shown below.

for (ushort objIndex = 0; objIndex < data.Length; ++objIndex)
{
    ushort length = sizeof(UInt16);
    data[objIndex] = (ushort)( (ushort)(byteArray[objIndex * length] << (ushort)8) +
                                             byteArray[objIndex + 1] );
}

Or this.

for (ushort objIndex = 0; objIndex < data.Length; ++objIndex)
{
    data[objIndex] = (ushort)((byteArray[objIndex * sizeof(UInt16)] << 8) +
                                                 byteArray[objIndex + 1] );
}
Dierdredieresis answered 26/12, 2013 at 16:55 Comment(0)
R
7

Check out Buffer.BlockCopy(). You can have the contents (byte[]) be dumped into a different format (UInt16[]).

Example:

var input = byte[1024];
var output = ushort[1024 / 2]; // every ushort is 2 bytes

Buffer.BlockCopy(input, 0, output, 0, 1024);
Rohrer answered 26/12, 2013 at 19:19 Comment(1)
Thanks. But I don't have Buffer.BlockCopy() (though I added the mscorlib.dll). I get the message "Name Buffer does not exist in the current context".Dierdredieresis
D
1

Finally it turned out to be a very simple fix (feeling silly right now). I was able to make the compiler error go away after casting each of the numbers I use to an ushort as shown below.

for (ushort objIndex = 0; objIndex < data.Length; ++objIndex)
{
    ushort length = sizeof(UInt16);
    data[objIndex] = (ushort)( (ushort)(byteArray[objIndex * length] << (ushort)8) +
                                             byteArray[objIndex + 1] );
}

Or this.

for (ushort objIndex = 0; objIndex < data.Length; ++objIndex)
{
    data[objIndex] = (ushort)((byteArray[objIndex * sizeof(UInt16)] << 8) +
                                                 byteArray[objIndex + 1] );
}
Dierdredieresis answered 26/12, 2013 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.