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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How do you want to handle numbers with odd numbers of digits?

I would first start with code like this to clean up the entry and make an even number of digits.

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

You say your code isn't working, but can you give us some examples where it fails, what the input value is, what is returned and what value you want returned.

You also might be running into problems where the function is used in a cell and excel is interpreting the string returned and automatically converting it to a number.
 
Upvote 0
I think the issue with the code happens anytime the hex value returns as an odd number
such as 3E8 the code only returns as E8 instead of E8 03. However I have no idea how to
fix this issue as of yet.
 
Upvote 0
How do you want to handle numbers with odd numbers of digits?

I would first start with code like this to clean up the entry and make an even number of digits.

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

You say your code isn't working, but can you give us some examples where it fails, what the input value is, what is returned and what value you want returned.

You also might be running into problems where the function is used in a cell and excel is interpreting the string returned and automatically converting it to a number.


The code does work without spacing between pairs, however it fails anytime the hex return is an odd value such as 3E8, A08EE and so forth. I would like for it to add the 0 in front of any odd value before returning the reverse Hex value and return the value with a space between each pair.
 
Upvote 0
I think the issue with the code happens anytime the hex value returns as an odd number
such as 3E8 the code only returns as E8 instead of E8 03. However I have no idea how to
fix this issue as of yet.
 
Upvote 0
How do you want to handle numbers with odd numbers of digits?

I would first start with code like this to clean up the entry and make an even number of digits.

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

You say your code isn't working, but can you give us some examples where it fails, what the input value is, what is returned and what value you want returned.

You also might be running into problems where the function is used in a cell and excel is interpreting the string returned and automatically converting it to a number.



And thank you very much for fixing the odd value to return with the beginning 0 as needed.
That fixed one of my main issue. Now if possible maybe maybe we can find a way for it to
return them with a space between the hex pairs.
 
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
 
Last edited:
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


This code works perfectly, I have tried many values and it always returns correct hex pair value. Thank you for all your help...

It is not really needed as I can use this code now to return the value, however it would be beneficial for me to only
use one Public function to do everything so if you have the time maybe we can alter the code to first find the hex
for a value then do what it already does. The code I use to find my hex value is as follows.

Code:
Function BigDec2Hex(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
  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
      BigDec2Hex = 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
    BigDec2Hex = BigDec2Hex & Mid$(HexValues, (4 + InStr(BinValues, "*" & Mid$(BinaryString, X, 4) & "*")) \ 5, 1)
  Next
  
For J = X - 1 To 0 Step 1
    Next J
  
End Function



"If possible I would like to combine the two into one simple public function"
 
Last edited by a moderator:
Upvote 0
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.
 
Last edited:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,407
Members
448,894
Latest member
spenstar

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