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


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

Examples:
  LongToHex(2001)       --> "000007D1"
  LongToHex(&H87654321) --> "87654321"

See LongToHexRev for the reversed-order function.
Code
LongToHex01
Public Function LongToHex01(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
  LongToHex01 = Right$("0000000" & Hex$(lLong), 8)
End Function
LongToHex02
Public Function LongToHex02(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
  Const Zero7 = "0000000"
  LongToHex02 = Right$(Zero7 & Hex$(lLong), 8)
End Function
LongToHex03
Public Static Function LongToHex03(lLong As Long) As String
' by Donald, donald@xbeat.net, 20010910
  LongToHex03 = "00000000"
  Mid$(LongToHex03, 9 - Len(Hex$(lLong))) = Hex$(lLong)
End Function
LongToHex04
Public Function LongToHex04(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(3) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  lBytes(2) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  lBytes(1) = lHex(lLong And 255)
  lLong = lLong \ &H100 ' right shift one byte
  
  If lHighBit Then lLong = lLong Or &H80
  lBytes(0) = lHex(lLong And 255)
  
  LongToHex04 = StringHelpers.SysAllocStringLen(lBytes(0), 8)
  
End Function
LongToHex05
Public Function LongToHex05(ByVal Value As Long) As String
' by Oskar Eisemuth, dev_oskar[at]hotmail.com, 20021011
' 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(0) = lHexLUT(((Value And &HFF000000) \ &H1000000) And &HFF)
  lHex(1) = lHexLUT((Value And &HFF0000) \ &H10000)
  lHex(2) = lHexLUT((Value And &HFF00&) \ &H100)
  lHex(3) = lHexLUT(Value And &HFF&)
  
  LongToHex05 = StringHelpers.SysAllocStringLen(lHex(0), 8)
End Function
Calls
1sRet = LongToHex(825580) --> "000C98EC"
Charts
 VB5 Charts
CodeAuthorDopingNotes
LongToHex01 Donald  
LongToHex02 Donald  
LongToHex03 Donald  
LongToHex04 PaulTLB 
LongToHex05 OskarTLB 
Call 1
33.110.489µs
43.150.496µs
53.640.573µs
21.030.162µs
11.000.158µs
 VB6 Charts
CodeAuthorDopingNotes
LongToHex01 Donald  
LongToHex02 Donald  
LongToHex03 Donald  
LongToHex04 PaulTLB 
LongToHex05 OskarTLB 
Call 1
53.500.569µs
43.470.565µs
33.410.556µs
21.010.164µs
11.000.163µs
Conclusions
...
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau