【问题标题】:Array.Copy error trying to convert single() to byte()Array.Copy 尝试将 single() 转换为 byte() 时出错
【发布时间】:2015-07-22 01:34:10
【问题描述】:

我想通过操作编辑一些音频/波形数据,我已将音频数据读取为 single()。

现在,经过处理,我想将其写入一个新的音频文件。 为此,我想将 singles() 写为 bytes()。

我正在尝试将 singles() 转换为 bytes(),但总是出错。

我正在尝试

Public Overridable Overloads Sub Write(ByVal uSingles() As Single)

    Dim nBytes(uSingles.Length * 4) As Byte

    Array.Copy(uSingles, nBytes, uSingles.Length)

    (...)

但是 Array.Copy 总是抛出错误。 有人看到我的错误吗? 谢谢。

【问题讨论】:

    标签: arrays vb.net type-conversion


    【解决方案1】:

    是的,它会抛出 TypeMismatchException,因为 srcArray 类型和 destArray 类型存在差异。实际上,这与复制single 数组无关。我认为您必须使用Stream (System.IO.MemoryStream) 才能生成byte() 数组。

    我建议是这样的:

    Public Function Write(ByVal uSingles() As Single) As Byte()
      Using ms As New MemoryStream
             Using bw As New BinaryWriter(ms)
               For Each no In uSingles
                  bw.Write(no)
               Next
            End Using
         Return ms.ToArray()
      End Using
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多