Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays BitConverter.ToInt16给出“;“目标阵列不够长”;_Arrays_Vb.net_Byte_Type Conversion - Fatal编程技术网

Arrays BitConverter.ToInt16给出“;“目标阵列不够长”;

Arrays BitConverter.ToInt16给出“;“目标阵列不够长”;,arrays,vb.net,byte,type-conversion,Arrays,Vb.net,Byte,Type Conversion,我正在尝试将字节数组转换为短数组,如下所示: Public Sub mixFinal() Dim patch1Buffer(patch1.Length - 44) As Byte System.Array.Copy(patch1, 44, patch1Buffer, 0, patch1.Length - 44) Dim patch1ShortBuffer(patch1Buffer.Length) As Short For x = 0 To patch1Buf

我正在尝试将字节数组转换为短数组,如下所示:

Public Sub mixFinal()
    Dim patch1Buffer(patch1.Length - 44) As Byte

    System.Array.Copy(patch1, 44, patch1Buffer, 0, patch1.Length - 44)

    Dim patch1ShortBuffer(patch1Buffer.Length) As Short

    For x = 0 To patch1Buffer.Length - 1 Step 1
        patch1ShortBuffer(x) = System.BitConverter.ToInt16(patch1Buffer, x)
    Next
End Sub
“patch1”是通过使用IO.file.ReadAllBytes方法读取.wav文件创建的字节数组

Visual Studio在编译程序时会出现以下错误:

Destination array is not long enough to copy all the items in the collection. Check array index and length.

我尝试将“patch1Buffer”和“patch1ShortBuffer”的大小更改为更高的值,但仍然出现错误。。。代码有什么问题?

A
short
字节大小等于2
bytes

因此,转换字节数组时产生的短路减少了两倍

以下代码将把字节数组转换为短字符:

Imports System.Runtime.InteropServices ' for alternative

Module Module1

    Sub Main()
        Dim bytes(1024-1) As Byte

        Dim shortSize = 2 ' alternative: shortCount = Marshal.SizeOf(GetType(Short))
        Dim shortCount = bytes.Length / shortSize

        Dim shorts(shortCount) as Short
        For i = 0 To shortCount-1
           shorts(i)= BitConverter.ToInt16(bytes, i*shortSize)
        Next

    End Sub

End Module

好的,我将假设你们正在尝试将单字节转换成短字节。。。。在这种情况下,bitconvertor.toint16函数将从patch1buffer的末尾运行,因为它一次检索2个字节

使用

相反

如果您试图在uShorts中获取这些对,则需要将patch1ShortBuffer的大小减半,使用x*2对ToInt16调用进行迭代,直至其长度

patch1ShortBuffer(x) = cShrt(patch1Buffer(x))