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
 
Something like this might help:

Code:
Sub pairsOfTwo()
Dim val As String, valLen As Long, i As Long, temp As String
val = "2DE426B4" 'make this equal to your hex value instead of being hardcoded in (after it has been made to have an even number of digits)
valLen = Len(val)

    For i = 2 To valLen Step 2
        temp = Mid(val, i - 1, 2)
        MsgBox temp
    Next i

End Sub



EDIT 21:38CST: Looks like I took too long to come up with my suggestion. Anyways, I'm glad you got a working solution.

EDIT2 21:40CST: Also, in the future if you can put your code between code tags it makes it much easier for others to read. You can either press the 'pound symbol' or 'hashtag symbol' to automatically generate opening and closing code tags or type code and /code with square brackets surrounding it, putting the 'code' one before your code and the '/code' one after your code.


Thank you for the advice and I will attempt to correct how I add code in my future posts.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How big a number are you talking about

Something like this would work with small enough numbers. If your values exceed the limits of Excel's Dec2Hex function, you'd have to write your own.

Code:
Function myFunction(aNumber as Long) as Long
    Dim hexString as String

    HexString = Application.Dec2Hex(aNumber)
    HexString = Hex_Pairs_Reverse(HexString)

    myFunction = Application.Hex2Dec(Replace(HexString," ",vbNullSTring)
End Function


Unfortunately this code did not help as it gave a very long return that was not even close to the hex value.
 
Upvote 0
Can you give me an example of a number, the desired result and the result that myFunction returned?
 
Upvote 0
Try
Code:
Public Function Hex_Pairs_Rev(ByVal strValue As String) As String
    Dim lngLoop As Long
    Dim strReturn As String

    strValue = Replace(strValue, " ", vbNullString)

    If Len(strValue) Mod 2 = 1 Then
        strValue = "0" & strValue
    End If

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

    Hex_Pairs_Rev = Mid(strReturn,2)
End Function

Here is another way to write this function...
Code:
Function Hex_Pairs_Rev(ByVal HexStr As String) As String
  Dim N As Long, Arr() As String
  Arr = Split(Trim(Format(HexStr, Application.Rept("@@ ", Len(HexStr)))))
  For N = UBound(Arr) To 0 Step -1
    Hex_Pairs_Rev = Trim(Hex_Pairs_Rev & " " & Right("00" & Arr(N), 2))
  Next
End Function
I see nothing wrong with maintaining two function... feeding the result of one into the other... that actually is desirable... each function does it own thing.
 
Last edited:
Upvote 0
Can you give me an example of a number, the desired result and the result that myFunction returned?


I started with a value of 1000 which should have then given 3E8 then reversed into pairs as E8 03 but the function returned a value of 59395 with no spaces
 
Upvote 0
1000 decimal = 03E8 in hex.
Reverse the pairs is E803
E803 in hex = 59395 decimal

That seems to be correct.

What is the result that you desire from 1000?
 
Upvote 0
1000 decimal = 03E8 in hex.
Reverse the pairs is E803
E803 in hex = 59395 decimal

That seems to be correct.


What is the result that you desire from 1000?


The desired result I was looking for would be the "E8 03" as the output.
 
Upvote 0
Then
Code:
Function myOtherFunction(aNumber as Long) as String
    Dim hexString as String

    HexString = Application.Dec2Hex(aNumber)
    MyOtherFunction = Hex_Pairs_Reverse(HexString)

End Function
 
Last edited:
Upvote 0
Solution
Here is another way to write this function...
Code:
Function Hex_Pairs_Rev(ByVal HexStr As String) As String
  Dim N As Long, Arr() As String
  Arr = Split(Trim(Format(HexStr, Application.Rept("@@ ", Len(HexStr)))))
  For N = UBound(Arr) To 0 Step -1
    Hex_Pairs_Rev = Trim(Hex_Pairs_Rev & " " & Right("00" & Arr(N), 2))
  Next
End Function
I see nothing wrong with maintaining two function... feeding the result of one into the other... that actually is desirable... each function does it own thing.


I agree that there is nothing wrong with using two separate functions as that is what I am doing at the moment.
I was just trying to see if there was a way to combine them into one function that returns the desired result.
 
Upvote 0
Then
Code:
Function myOtherFunction(aNumber as Long) as String
    Dim hexString as String

    HexString = Application.Dec2Hex(aNumber)
    MyOtherFunction = Hex_Pairs_Reverse(HexString)

End Function


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

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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