vb6 - 将大字节数组转换为长数组会抛出溢出异常

标签 vb6 bytearray

我第一次开发 vb 6.0 应用程序 我正在尝试将大小为 (164999) 的巨大字节数组转换为 VB 6.0 中的长/整数数组,但它给了我一个溢出错误。

我的代码

Dim tempByteData() As Byte // of size (164999)
Dim lCount As Long

Private Sub Open()
    lCount = 164999 **//here I get the count i.e. 164999**                     
    ReDim tempByteData(lCount - 1)
    For x = 0 To obj.BinaryValueCount - 1
        tempwaveformData(x) = CByte(obj.BinaryValues(x))
    Next
    tempByteData(lCount - 1) = BArrayToInt(tempByteData)   
End Sub

Private Function BArrayToInt(ByRef bArray() As Byte) As Long
    Dim iReturn() As Long
    Dim i As Long

    ReDim iReturn(UBound(bArray) - 1)
    For i = 0 To UBound(bArray) - LBound(bArray)
        iReturn(i) = iReturn(i) + bArray(i) * 2 ^ i
    Next i

    BArrayToInt = iReturn(i)

End Function

需要做些什么才能将所有字节数组数据转换为长/整数数组或除此之外的任何替代方法来存储这些字节数组,从而不会发生溢出异常

最佳答案

根据字节数组中 32 位数据的布局,您可以直接从一个数组到另一个数组进行内存复制。

这仅在数据为小端时有效(对于 Win32 应用程序/数据是正常的,但并不总是保证)

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Function ByteArrayToLongArray(ByRef ByteArray() As Byte) As Long()
Dim LongArray() As Long
Dim Index As Long

  'Create a Long array big enough to hold the data in the byte array
  'This assumes its length is a multiple of 4.
  ReDim LongArray(((UBound(ByteArray) - LBound(ByteArray) + 1) / 4) - 1)

  'Copy the data wholesale from one array to another
  CopyMemory LongArray(LBound(LongArray)), ByteArray(LBound(ByteArray)), UBound(ByteArray) + 1

  'Return the resulting array
  ByteArrayToLongArray = LongArray
End Function

但是,如果数据是大端,那么您需要一次转换每个字节:

Private Function ByteArrayToLongArray(ByRef ByteArray() As Byte) As Long()
Dim LongArray() As Long
Dim Index As Long

  'Create a Long array big enough to hold the data in the byte array
  'This assumes its length is a multiple of 4.
  ReDim LongArray(((UBound(ByteArray) - LBound(ByteArray) + 1) / 4) - 1)

  'Copy each 4 bytes into the Long array
  For Index = LBound(ByteArray) To UBound(ByteArray) Step 4
    'Little endian conversion
    'LongArray(Index / 4) = (ByteArray(Index + 3) * &H1000000&) Or (ByteArray(Index + 2) * &H10000&) Or (ByteArray(Index + 1) * &H100&) Or (ByteArray(Index))
    'Big endian conversion
    LongArray(Index / 4) = (ByteArray(Index) * &H1000000&) Or (ByteArray(Index + 1) * &H10000&) Or (ByteArray(Index + 2) * &H100&) Or (ByteArray(Index + 3))
  Next

  'Return the resulting array
  ByteArrayToLongArray = LongArray
End Function

(如果每个四边形的第一个字节大于表示负数的 127,则此示例目前会中断。)

关于vb6 - 将大字节数组转换为长数组会抛出溢出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16934514/

相关文章:

c++ - Qt4 : write QByteArray to file with filename?

作为标准用户在 Windows Vista 上安装 VB6

postgresql - PostgreSQL ODBC连接SSPI身份验证失败

java - 将 VB6 Dbl 日期值转换为 Java 长值

c# - byte[] 到 silverlight 中的 BitmapImage

c# - 如何使用字节数组比较两个图像

c++ - 什么 vb6 类型与 std::vector 兼容 ABI?

excel - VB 6 程序中的 MS Office 14.0 对象库引用

actionscript-3 - 如何从 ActionScript 3 中的缓冲区 (ByteArray/Stream) 播放 MP3 声音?

java - 如何将字节数组写入和读取到 DataInput 和 DataOutput Stream