将大型字节数组转换为长整型数组会抛出溢出异常。

3

我是你的助手,把以下内容翻译为中文:

这是我第一次开发 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

如何将所有字节数组数据转换为长整型/整型数组或任何其他存储这些字节数组的替代方法,以避免发生溢出异常。


1
164999是它的大小还是上限?请记住VB6默认使用下限为0,因此ReDim tempByteData(164999)将产生一个大小为165000的数组。您的示例似乎也正在丢弃结果为32位的数组。您还在使用逐渐递增的值i来移动字节值,一旦达到约24就会溢出。 - Deanna
你知道32位的值在字节数组中是如何表示的吗?是Windows使用的普通小端,还是TCP协议栈和一些网络协议使用的大端呢?(1的值会显示为 01 00 00 00 还是 00 00 00 01)也许有一种方法可以直接在一次调用中进行复制,而无需对每个字节进行计算。 - Deanna
@Deanna 我刚刚更新了代码,UBound(bArray) = 164999,LBound(bArray) = 0,是的,你是正确的,当数组指向(24)位置时,数组会被丢弃,那么需要做出哪些更正呢........ - user2404595
我现在明白你从哪里得到了你的示例。那只是将一个4字节数组转换为单个整数。 - Deanna
1个回答

4

根据字节数组中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时会出现错误,这表示一个负数。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接