在VB.Net中将字节数组转换为短裤(Int16) [英] Converting Byte Array into Shorts (Int16) in VB.Net

查看:199
本文介绍了在VB.Net中将字节数组转换为短裤(Int16)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Vb.net中将字节数组转换为短值数组?请注意,我想从给定的字节数组中读取每组2个字节值作为单个short值.请提出建议.
请注意,我需要从包含wav样本作为字节值(例如255、255、0、2等)的文本文件中写入wav文件. >使用此:

  Dim 字节 As   Byte ()
短裤= Array.ConvertAll(bytes,功能(b) CShort (b))


这并不困难:只需要一点思考即可!
C#版本:

  byte  [] bytes = File.ReadAllBytes( @"  D:\ Temp \ MyPic.jpg");
短裤 []短裤=  短裤 [ (bytes.Length&  1 )==  0 吗? bytes.Length/ 2 :(bytes.Length/ 2 )+  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 ));
    }
如果((bytes.Length&  1 )!=  0 )
    {
    shorts [j] =( short )bytes [bytes.Length- 1 ];
    } 


或VB:

  Dim 字节 As   Byte ()= File.ReadAllBytes(" )
短裤 As  短裤()= 新建 简短(如果((bytes.Length  And   1 )=  0 ,字节.长度\  2 ,(字节数.长度\  2 )+  1 )- 1 ){}
 Dim  j  As  整数 =  0 
对于 i  As  整数 =  0  字节.长度- 1  步骤  2 
     Dim  u1  As   UInteger  =  CUInt (bytes(i)) And & Hff
     Dim  u2  As   UInteger  =  CUInt (bytes(i +  1 )) And & ;高音
    shorts(j)=  CShort (u2  Or (u1<<  1 
下一步
如果(字节数.长度 And   1 ) <>  0  然后
    短裤(j)=  CShort (bytes(bytes.Length- 1 ))
结束 如果 

我已经在C#中进行了测试,并将其自动转换为VB.

您可能要在行中反转u1和u2:

短裤(j)=  CShort (u1  Or (u2<< ;  8 ))

(如果您想要大字节序而不是小字节序)(即,如果您的数据具有低字节秒)


现在无需进行转换.我使用memorystream来完成任务.感谢您的意见,尤其是OriginalGriff.


How do I convert a Byte Array into an array of short values in Vb.net? Note that I want to read each set of 2 byte values from the given byte array as a single short value. Please suggest.
Note that I need this for writing wav file from a textfile that contains wav samples as byte values (such as 255, 255, 0, 2, etc.).

解决方案

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.


这篇关于在VB.Net中将字节数组转换为短裤(Int16)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆