VBspeed / Bits / LongToHexRev
VBspeed © 2000-10, updated: 12-Oct-2002
LongToHexRev


Function LongToHexRev
Returns the reversed hexadecimal representation of a specified Long (4 bytes = 8 hex chars, most significant byte right). Zeroes are right-padded (that's the difference to VB's Hex$() function).

Examples:
  LongToHexRev(2001)       --> "D1070000"
  LongToHexRev(&H87654321) --> "21436587"

See LongToHex for the non-reversed-order function.
Code
LongToHexRev01
Public Function LongToHexRev01(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
  Static sTmp As String
  Dim i As Long
  ' to hex, left-padd zeroes
  sTmp = Right$("0000000" & Hex$(lLong), 8)
  LongToHexRev01 = sTmp
  ' reverse byte order
  For i = 1 To 7 Step 2
    Mid$(LongToHexRev01, 8 - i) = Mid$(sTmp, i, 2)
  Next
End Function
LongToHexRev02
Public Function LongToHexRev02(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
' API-declared RtlMoveMemory
  Dim bCol(3) As Byte
  Dim lLongRev As Long
  Dim i As Long
  ' reverse byte order
  For i = 0 To 3
    RtlMoveMemory bCol(3 - i), ByVal VarPtr(lLong) + i, 1
  Next
  RtlMoveMemory lLongRev, bCol(0), 4
  ' to hex, left-padd zeroes
  LongToHexRev02 = Right$("0000000" & Hex$(lLongRev), 8)
End Function
LongToHexRev03
Public Function LongToHexRev03(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
' TLB-declared CopyMemory
  Dim bCol(3) As Byte
  Dim lLongRev As Long
  Dim i As Long
  ' reverse byte order
  For i = 0 To 3
    CopyMemory bCol(3 - i), ByVal VarPtr(lLong) + i, 1
  Next
  CopyMemory lLongRev, bCol(0), 4
  ' to hex, left-padd zeroes
  LongToHexRev03 = Right$("0000000" & Hex$(lLongRev), 8)
End Function
LongToHexRev04
Public Function LongToHexRev04(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
  Dim lLongRev As Long
  Dim HiWord As Integer
  Dim LoWord As Integer
  
  ' extract hiword and loword inline
  HiWord = (lLong And &HFFFF0000) \ &H10000
  If lLong And &H8000& Then
    LoWord = lLong Or &HFFFF0000
  Else
    LoWord = lLong And &HFFFF&
  End If
  ' swap bytes
  HiWord = ByteSwap01(HiWord)
  LoWord = ByteSwap01(LoWord)
  ' swap words
  lLongRev = (LoWord * &H10000) Or (HiWord And &HFFFF&)
    ' to hex, left-padd zeroes
  LongToHexRev04 = Right$("0000000" & Hex$(lLongRev), 8)
End Function

Public Function ByteSwap01(w As Integer) As Integer ' by Donald, donald@xbeat.net, 20010910 Dim LoByte As Byte Dim HiByte As Byte LoByte = w And &HFF HiByte = (w And &HFF00&) \ &H100 If LoByte And &H80 Then ByteSwap01 = ((LoByte * &H100&) Or HiByte) Or &HFFFF0000 Else ByteSwap01 = (LoByte * &H100) Or HiByte End If End Function
LongToHexRev05
Public Function LongToHexRev05(ByVal lLong As Long) As String
' by Paul, wpsjr1@syix.com, 20010911
' TLB - SysAllocStringLen (below StringHelpers = split03.tlb)
  
  Static lBytes(3) As Long
  Static lHex(255) As Long
  Dim sHex As String
  Static i As Long
  Dim lHighBit As Long
  
  If i Then
    ' do nothing
  Else
    For i = 0 To 255
     sHex = Right$("00" & Hex$(i), 2)
     lHex(i) = (Asc(Right$(sHex, 1)) * &H10000) Or Asc(sHex)
    Next i
  End If
  
  lHighBit = lLong And &H80000000
  If lHighBit Then lLong = lLong And Not &H80000000
  
  lBytes(0) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  lBytes(1) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  lBytes(2) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  If lHighBit Then lLong = lLong Or &H80
  lBytes(3) = lHex(lLong And 255)
  
  LongToHexRev05 = StringHelpers.SysAllocStringLen(lBytes(0), 8)
End Function
LongToHexRev06
Public Function LongToHexRev06(ByVal Value As Long) As String
' by Oskar Eisemuth, dev_oskar[at]hotmail.com, 20021012
' TLB - SysAllocStringLen (StringHelpers: Split03.tlb)
  Static lHex(0 To 3) As Long
  Static lHexLUT(255) As Long
  Static i As Long
  Dim sHex As String
  
  If i Then
    ' Nothing here
  Else
    For i = 0 To 15
      lHexLUT(i) = (Asc(Hex$(i)) * &H10000) Or Asc("0")
    Next i
    For i = 16 To 255
      sHex = Hex$(i)
      lHexLUT(i) = (Asc(Right$(sHex, 1)) * &H10000) Or Asc(sHex)
    Next i
  End If
  
  lHex(3) = lHexLUT(((Value And &HFF000000) \ &H1000000) And &HFF)
  lHex(2) = lHexLUT((Value And &HFF0000) \ &H10000)
  lHex(1) = lHexLUT((Value And &HFF00&) \ &H100)
  lHex(0) = lHexLUT(Value And &HFF&)
  
  LongToHexRev06 = StringHelpers.SysAllocStringLen(lHex(0), 8)
End Function
Calls
1sRet = LongToHex(825580) --> "EC980C00"
Charts
 VB5 Charts
CodeAuthorDopingNotes
LongToHexRev01 Donald  
LongToHexRev02 DonaldAPI 
LongToHexRev03 DonaldTLB 
LongToHexRev04 Donald  
LongToHexRev05 PaulTLB 
LongToHexRev06 OskarTLB 
Call 1
610.321.561Ás
59.361.416Ás
49.271.402Ás
33.300.499Ás
21.070.161Ás
11.000.151Ás
 VB6 Charts
CodeAuthorDopingNotes
LongToHexRev01 Donald  
LongToHexRev02 DonaldAPI 
LongToHexRev03 DonaldTLB 
LongToHexRev04 Donald  
LongToHexRev05 PaulTLB 
LongToHexRev06 OskarTLB 
Call 1
610.471.702Ás
59.331.515Ás
49.151.487Ás
33.500.569Ás
21.020.165Ás
11.000.162Ás
Conclusions
...
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau