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 |
1 | sRet = LongToHex(825580) --> "EC980C00"
|
Charts |
VB5 Charts |
|
Call 1 |
6 | 10.32 | 1.561µs |
5 | 9.36 | 1.416µs |
4 | 9.27 | 1.402µs |
3 | 3.30 | 0.499µs |
2 | 1.07 | 0.161µs |
1 | 1.00 | 0.151µs |
|
|