Reverse Hex Byte Pairs

rdrjr

New Member
Joined
Sep 28, 2019
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Okay this is what I am trying to do. I need to take a Hex value and have it returned in reverse Byte pairs as in the following diagram

For instance the top line (480000074B26E42D) being the original hex value, then reversing it so that the outcome = 2D E4 26 48 07 00 00 48

nj8PD.png

I have tried using the following code however the result I get is not what I am needing. It starts out fine but gets all jumbled up somewhere in the middle. As a most welcomed plus it also should be able to run as small as 1 Hex pair to as large as 16 hex pair. And would be even more beneficial if it could also find the hex value from a value such as value 1000 = hex value E8 03 taking note that the E8 03 value is already reversed as the original would have been 3E8 or 03E8


Public Function Hex_Pairs_Rev(ByVal strValue As String) As String


Dim lngLoop As Long
Dim strReturn As String


strReturn = ""


For lngLoop = Len(strValue) - 1& To 1& Step -2&
strReturn = strReturn & Mid$(strValue, lngLoop, 2)
Next lngLoop


Hex_Pairs_Rev = strReturn
End Function
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
Function myOtherFunction(aNumber as Long) as String
    Dim hexString as String

    HexString = Application.Dec2Hex(aNumber)
    MyOtherFunction = [B][COLOR="#FF0000"]Hex_Pairs_Reverse[/COLOR][/B](HexString)

End Function
This looks as though it would indeed work, at the moment it is giving me a #VALUE ! error

Mike changed the name of his reversal function that the above function is calling (instead of ending in "Rev" here it ends in "Reverse"). Change what I highlighted in red to the name of the reversal function you are using.
 
Last edited:
Upvote 0
Mike changed the name of his reversal function that the above function is calling (instead of ending in "Rev" here it ends in "Reverse"). Change what I highlighted in red to the name of the reversal function you are using.


These are the two functions that I am using and I still get a #VALUE ! error

Code:
Function myOtherFunction(aNumber As Long) As Long


    Dim hexString As String


    hexString = Application.Dec2Hex(aNumber)
    myOtherFunction = Hex_Pairs_Rev(hexString)


End Function


and my Hex_Pairs_Rev

Code:
Public Function Hex_Pairs_Rev(ByVal strValue As String) As String
    Dim lngLoop         As Long
    Dim strReturn       As String


    strValue = Replace(strValue, " ", vbNullString)     'used to clean up the odd value
        If Len(strValue) Mod 2 = 1 Then                 'this code adds a 0 to the begining
            strValue = "0" & strValue                   'of the hex value to change the
        End If                                          'odd value into an even value


    strReturn = ""
    For lngLoop = Len(strValue) - 1& To 1& Step -2&               'code to reverse
        strReturn = strReturn & " " & Mid$(strValue, lngLoop, 2)  'the hex byte
    Next lngLoop                                                  'by pairs


    Hex_Pairs_Rev = Mid(strReturn, 2)    'This returns the reversed hex byte with a space between each hex pair
End Function
 
Upvote 0
found the error @
Code:
Function myOtherFunction(aNumber As Long) As Long
'should be
Function myOtherFunction(aNumber As Long) As String
 
Upvote 0
I just wanted to thank both Mike and Rick for all their help with this function. And anyone else that helped as well.
 
Upvote 0
I'm late on the scene, but as an exercise I thought I would see if it was possible to reverse the hex string without looping (though nothing wrong with the small number of loops that would be required here. ;))

I have combined the reversing into your BigDec2Hex function (which I have not checked but you seem comfortable with) as requested. Apart from changing the name of the function throughout, I have highlighted what I have added.

Rich (BB code):
Function BigDec2HexAndReverse(ByVal DecimalIn As Variant, Optional BitSize As Long = 93) As String
  Dim J As Integer
  Dim x As Integer, PowerOfTwo As Variant, BinaryString As String
  Dim HexBits As Variant
  Const BinValues = "*0000*0001*0010*0011*0100*0101*0110*0111*1000*1001*1010*1011*1100*1101*1110*1111*"
  Const HexValues = "0123456789ABCDEF"
  DecimalIn = Int(CDec(DecimalIn))
  If DecimalIn < 0 Then
    If BitSize > 0 Then
      PowerOfTwo = 1
      For x = 1 To BitSize
        PowerOfTwo = 2 * CDec(PowerOfTwo)
      Next
    End If
    DecimalIn = PowerOfTwo + DecimalIn
    If DecimalIn < 0 Then
      BigDec2HexAndReverse = CVErr(xlErrValue)
      Exit Function
    End If
  End If
  Do While DecimalIn <> 0
    BinaryString = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & BinaryString
    DecimalIn = Int(DecimalIn / 2)
  Loop
  BinaryString = String$((4 - Len(BinaryString) Mod 4) Mod 4, "0") & BinaryString
  For x = 1 To Len(BinaryString) - 3 Step 4
    BigDec2HexAndReverse = BigDec2HexAndReverse & Mid$(HexValues, (4 + InStr(BinValues, "*" & Mid$(BinaryString, x, 4) & "*")) \ 5, 1)
  Next
  BigDec2HexAndReverse = String(Len(BigDec2HexAndReverse) Mod 2, "0") & BigDec2HexAndReverse '<- Add 0 if odd length
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(.{2})(?!$)"
    HexBits = Split(.Replace(BigDec2HexAndReverse, "$1 "))
    BigDec2HexAndReverse = Join(Application.Index(HexBits, 1, Application.Transpose(Evaluate(UBound(HexBits) + 2 & "- row(1:" & UBound(HexBits) + 1 & ")"))), " ")
  End With
End Function
 
Upvote 0
Hi
I've got this Idea you may try it
for one cell
range("a1")=480000074B26E42D


Code:
Sub tst()
    Dim x As Variant
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[\d^\[A-F]{2}"
        If .test(Cells(1, 1)) Then
            Set m = .Execute(Cells(1, 1))
            ReDim x(1 To m.Count)
            t = 1
            For i = m.Count - 1 To 0 Step -1
                x(t) = m(i)
                t = t + 1
            Next i
            Cells(2, 1) = Join(x, "")
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Correction
Code:
Sub tst()
    Dim x As Variant
    Dim t, m, i
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[\d^\[A-F]{1,2}"
        If .test(Cells(1, 1)) Then
            Set m = .Execute(Cells(1, 1))
            ReDim x(1 To m.Count)
            t = 1
            For i = m.Count - 1 To 0 Step -1
                x(t) = m(i)
                t = t + 1
            Next i
            Cells(2, 1) = Join(x, "")
        End If
    End With
End Sub
 
Last edited:
Upvote 0
I'm late on the scene, but as an exercise I thought I would see if it was possible to reverse the hex string without looping (though nothing wrong with the small number of loops that would be required here. ;))

I have combined the reversing into your BigDec2Hex function (which I have not checked but you seem comfortable with) as requested. Apart from changing the name of the function throughout, I have highlighted what I have added.

Rich (BB code):
Function BigDec2HexAndReverse(ByVal DecimalIn As Variant, Optional BitSize As Long = 93) As String
  Dim J As Integer
  Dim x As Integer, PowerOfTwo As Variant, BinaryString As String
  Dim HexBits As Variant
  Const BinValues = "*0000*0001*0010*0011*0100*0101*0110*0111*1000*1001*1010*1011*1100*1101*1110*1111*"
  Const HexValues = "0123456789ABCDEF"
  DecimalIn = Int(CDec(DecimalIn))
  If DecimalIn < 0 Then
    If BitSize > 0 Then
      PowerOfTwo = 1
      For x = 1 To BitSize
        PowerOfTwo = 2 * CDec(PowerOfTwo)
      Next
    End If
    DecimalIn = PowerOfTwo + DecimalIn
    If DecimalIn < 0 Then
      BigDec2HexAndReverse = CVErr(xlErrValue)
      Exit Function
    End If
  End If
  Do While DecimalIn <> 0
    BinaryString = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & BinaryString
    DecimalIn = Int(DecimalIn / 2)
  Loop
  BinaryString = String$((4 - Len(BinaryString) Mod 4) Mod 4, "0") & BinaryString
  For x = 1 To Len(BinaryString) - 3 Step 4
    BigDec2HexAndReverse = BigDec2HexAndReverse & Mid$(HexValues, (4 + InStr(BinValues, "*" & Mid$(BinaryString, x, 4) & "*")) \ 5, 1)
  Next
  BigDec2HexAndReverse = String(Len(BigDec2HexAndReverse) Mod 2, "0") & BigDec2HexAndReverse '<- Add 0 if odd length
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(.{2})(?!$)"
    HexBits = Split(.Replace(BigDec2HexAndReverse, "$1 "))
    BigDec2HexAndReverse = Join(Application.Index(HexBits, 1, Application.Transpose(Evaluate(UBound(HexBits) + 2 & "- row(1:" & UBound(HexBits) + 1 & ")"))), " ")
  End With
End Function



Thank you very much, this code works very well and was actually what I was looking for when I stated that I would like to combine the two Public Functions into one that does everything. Now I can simplify my spreadsheet and have only one function for both small and large values. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top