3

First time I am developing the vb 6.0 application I am trying to convert huge Byte Array of size(164999) into Long/Integer Array in VB 6.0 but it gives me an Overflow Error.

my code

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

what needs to be done so that all Byte Array data is converted into Long/Integer Array or any alternate way than this to stored these byte array so that overflow exception not occurred

4
  • 1
    Is 164999 its size or upper bound? Remember that VB6 uses (by default) a lower bound of 0 so ReDim tempByteData(164999) results in a 165000 size array. Your sample also seems to be discarding the resulting 32-bit array. You're also using an ever incrementing value for i to shift the byte value which will overflow once it gets to about 24. Commented Jun 5, 2013 at 10:01
  • Do you know how the 32-bit values are represented in the byte array? normal little endian used by Windows, or big endian used by the TCP stack and some network protocols? (does the values of 1 appear as 01 00 00 00 or 00 00 00 01) There may be a way to copy directly in one call with no calculation on each byte) Commented Jun 5, 2013 at 10:29
  • @Deanna I just update the code UBound(bArray) = 164999 and LBound(bArray) = 0 yes you are correct array become discarding when array pointing to(24) location so what corrections are required........ Commented Jun 5, 2013 at 10:31
  • I see where you got your sample from now. That only converts a 4 byte array to a single integer. Commented Jun 5, 2013 at 10:53

1 Answer 1

4

Depending on the layout of the 32-bit data in the byte array, you can do a straight memory copy from one array to the other.

This will only work if the data is little endian (normal for Win32 applications/data, but not always guaranteed)

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

If however the data is big endian, then you will need to convert each byte at a time:

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

(This example currently breaks if the first byte of each quad is greater than 127 which signifies a negative number.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.