Quantcast
Viewing all articles
Browse latest Browse all 1449

Debug Byte Array

I use byte arrays a lot, and traditionally I have used the following routine to display the results in the Immediate window.
Code:

Private Sub DebugPrintByteOrg(sDescr As String, bArray() As Byte)
    Dim lPtr As Long
    Debug.Print sDescr & ":"
    For lPtr = 0 To UBound(bArray)
        Debug.Print Right$("0" & Hex$(bArray(lPtr)), 2) & " ";
        If (lPtr + 1) Mod 16 = 0 Then Debug.Print
    Next lPtr
    Debug.Print
End Sub

This is not a speedy process, so I attempted to make a better one.
Code:

Private Sub DebugPrintByte(sDescr As String, bData() As Byte)
    Const rSize        As Long = 16 'Bytes per row
    Dim lSize          As Long
    Dim Row            As String
    Dim X              As Long
    Dim Y              As Long
    lSize = GetbSize(bData)
    Debug.Print sDescr & ":"
    If lSize = 0 Then Exit Sub
    On Error Resume Next
    For Y = 0 To Int(lSize / rSize)
        Row = String$(rSize * 3, " ")
        For X = 0 To rSize - 1
            Mid$(Row, (X * 3) + 1, 2) = Right$("0" & Hex$(bData(Y * rSize + X)), 2)
        Next
        Row = RTrim$(Row) & " "
        Debug.Print Row
    Next
 End Sub

Private Function GetbSize(bArray() As Byte) As Long
    On Error GoTo GetSizeErr
    GetbSize = UBound(bArray) + 1
    Exit Function
GetSizeErr:
    GetbSize = 0
End Function

Although speed is not critical in the IDE, this one turned out to be twice as fast, checks for a null array, and lets you control the width of the output.

J.A. Coutts

Viewing all articles
Browse latest Browse all 1449

Trending Articles