locked
excel vba parse long number string into 4 bytes per row RRS feed

  • Question

  • I have a long hex string with no delimiters. I would like to create a button and convert the long row (A1) into rows of numbers 4 long (byte) and populate the next free row with the next 4 numbers. String length appears to be a constant @ 522 char.

    Example : "583031303A023A023A023A02620262026202620251025........"

    5830

    3130

    03a0...


    heads up

    Friday, March 28, 2014 5:07 PM

All replies

  • One of many ways -

    Sub test()
    Dim i As Long, j As Long
    Dim n As Long, q As Long
    Dim s As String, t As String
    Dim arr() As String
    
        For i = 1 To 24
            s = s & Chr(64 + i)
        Next
    
        n = Len(s)
        q = Int((n - 1) / 4)
        ReDim arr(0 To q, 0 To 1) As String
    
        For i = n To 1 Step -1
            j = j + 1
            t = Mid$(s, i, 1) & t
            If j = 4 Or i = 1 Then
                arr(q, 0) = t
                arr(q, 1) = n - i + 1 & " to " & n - i - j + 2
                j = 0
                q = q - 1
                t = ""
            End If
        Next
    
        For i = 0 To UBound(arr)
            Debug.Print arr(i, 0), arr(i, 1)
        Next
    
         Range("A1").Resize(UBound(arr) + 1, 2) = arr
    End Sub
    

    Peter Thornton

    Friday, March 28, 2014 8:30 PM