且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在VB.Net中将字节数组转换为短裤(Int16)

更新时间:2022-10-22 16:17:44

Use this:

Dim bytes As Byte()
Dim shorts = Array.ConvertAll(bytes, Function(b) CShort(b))


It''s not difficult: it just takes a little thinking!
The C# version:
byte[] bytes = File.ReadAllBytes(@"D:\Temp\MyPic.jpg");
short[] shorts = new short[(bytes.Length & 1) == 0 ? bytes.Length / 2 : (bytes.Length / 2) + 1];
int j = 0;
for (int i = 0; i < bytes.Length; i += 2)
    {
    uint u1 = (uint)bytes[i] & 0xFF;
    uint u2 =  (uint)bytes[i + 1] & 0xFF;
    shorts[j++] = (short) (u1 | (u2 << 8));
    }
if ((bytes.Length & 1) != 0)
    {
    shorts[j] = (short)bytes[bytes.Length - 1];
    }


Or VB:

Dim bytes As Byte() = File.ReadAllBytes("D:\Temp\MyPic.jpg")
Dim shorts As Short() = New Short(If((bytes.Length And 1) = 0, bytes.Length \ 2, (bytes.Length \ 2) + 1) - 1) {}
Dim j As Integer = 0
For i As Integer = 0 To bytes.Length - 1 Step 2
    Dim u1 As UInteger = CUInt(bytes(i)) And &Hff
    Dim u2 As UInteger = CUInt(bytes(i + 1)) And &Hff
    shorts(j) = CShort(u2 Or (u1 << 8))
    j = j + 1
Next
If (bytes.Length And 1) <> 0 Then
    shorts(j) = CShort(bytes(bytes.Length - 1))
End If

I''ve tested in in C# and auto converted it to VB.

You may want to reverse u1 and u2 in the line:

shorts(j) = CShort(u1 Or (u2 << 8))

if you want big endian instead of little endian (i.e. if your data has the low byte second)


No conversion needed now. I used memorystream to accomplish my task. Thanks to all for your comments, especially OriginalGriff.