VB6Parse / Library / Conversion / hex

VB6 Library Reference

Hex Function

Returns a String representing the hexadecimal value of a number.

Syntax

Hex(number)

Parameters

Return Value

Returns a String representing the hexadecimal value of the number. The return value contains hexadecimal digits (0-9, A-F) without the "&H" prefix.

Remarks

The Hex function converts a decimal number to its hexadecimal (base 16) string representation:

Typical Uses

  1. Color Values: Convert RGB color values to hexadecimal for HTML/CSS
  2. Memory Addresses: Display memory addresses in hexadecimal format
  3. Debugging: Show binary data in readable hexadecimal format
  4. Low-Level Programming: Work with hardware registers and bit patterns
  5. File I/O: Display byte values when working with binary files
  6. Cryptography: Show hash values and checksums in hex format

Basic Usage Examples

' Example 1: Simple conversion
Debug.Print Hex(255)        ' Prints: FF
Debug.Print Hex(16)         ' Prints: 10
Debug.Print Hex(0)          ' Prints: 0

' Example 2: Color conversion
Dim red As Long
red = RGB(255, 0, 0)
Debug.Print Hex(red)        ' Prints: FF (on little-endian systems)

' Example 3: Negative numbers (two's complement)
Debug.Print Hex(-1)         ' Prints: FFFFFFFF (Long)
Debug.Print Hex(-256)       ' Prints: FFFFFF00

' Example 4: With "&H" prefix
Dim value As Long
value = 42
Debug.Print "&H" & Hex(value)  ' Prints: &H2A

Common Patterns

' Pattern 1: RGB to hex color string
Function RGBToHex(r As Integer, g As Integer, b As Integer) As String
RGBToHex = Right$("0" & Hex(r), 2) & _
Right$("0" & Hex(g), 2) & _
Right$("0" & Hex(b), 2)
End Function

' Pattern 2: Pad hex string to specific length
Function HexPad(value As Long, length As Integer) As String
HexPad = Right$(String$(length, "0") & Hex(value), length)
End Function

' Pattern 3: Display memory dump
Sub ShowMemoryDump(bytes() As Byte)
Dim i As Long
Dim line As String
For i = LBound(bytes) To UBound(bytes)
line = line & Right$("0" & Hex(bytes(i)), 2) & " "
If (i + 1) Mod 16 = 0 Then
Debug.Print line
line = ""
End If
Next i
If Len(line) > 0 Then Debug.Print line
End Sub

' Pattern 4: Format address with hex
Function FormatAddress(addr As Long) As String
FormatAddress = "0x" & Right$("00000000" & Hex(addr), 8)
End Function

' Pattern 5: Convert byte array to hex string
Function BytesToHex(bytes() As Byte) As String
Dim result As String
Dim i As Long
For i = LBound(bytes) To UBound(bytes)
result = result & Right$("0" & Hex(bytes(i)), 2)
Next i
BytesToHex = result
End Function

' Pattern 6: Parse hex string back to number
Function HexToLong(hexStr As String) As Long
If Left$(hexStr, 2) = "&H" Then
HexToLong = CLng(hexStr)
Else
HexToLong = CLng("&H" & hexStr)
End If
End Function

' Pattern 7: Show bit pattern
Sub ShowBitPattern(value As Long)
Debug.Print "Hex: " & Hex(value)
Debug.Print "Dec: " & value
End Sub

' Pattern 8: Color component extraction
Function GetRedComponent(color As Long) As Integer
GetRedComponent = color And &HFF
Debug.Print "Red: " & Hex(GetRedComponent)
End Function

' Pattern 9: Build lookup table
Dim hexLookup(0 To 255) As String
Sub InitHexLookup()
Dim i As Long
For i = 0 To 255
hexLookup(i) = Right$("0" & Hex(i), 2)
Next i
End Sub

' Pattern 10: Format hex with separator
Function HexWithSeparator(value As Long, separator As String) As String
Dim hexStr As String
Dim i As Integer
hexStr = Right$("00000000" & Hex(value), 8)
For i = 1 To 7 Step 2
HexWithSeparator = HexWithSeparator & Mid$(hexStr, i, 2)
If i < 7 Then HexWithSeparator = HexWithSeparator & separator
Next i
End Function

Advanced Usage Examples

' Example 1: Hex dump utility class
Public Class HexDumper
Private Const BYTES_PER_LINE As Integer = 16

Public Function DumpToString(data() As Byte) As String
Dim result As String
Dim i As Long
Dim offset As Long
Dim hexPart As String
Dim asciiPart As String
Dim b As Byte

For i = LBound(data) To UBound(data)
If i Mod BYTES_PER_LINE = 0 Then
If i > 0 Then
result = result & hexPart & "  " & asciiPart & vbCrLf
End If
hexPart = Right$("00000000" & Hex(i), 8) & ": "
asciiPart = ""
End If

b = data(i)
hexPart = hexPart & Right$("0" & Hex(b), 2) & " "

If b >= 32 And b <= 126 Then
asciiPart = asciiPart & Chr$(b)
Else
asciiPart = asciiPart & "."
End If
Next i

' Pad last line
If Len(hexPart) > 10 Then
hexPart = hexPart & String$((BYTES_PER_LINE - Len(asciiPart)) * 3, " ")
result = result & hexPart & "  " & asciiPart
End If

DumpToString = result
End Function
End Class

' Example 2: HTML color converter
Public Class ColorConverter
Public Function VBColorToHTML(vbColor As Long) As String
Dim r As Integer, g As Integer, b As Integer
r = vbColor And &HFF
g = (vbColor \ &H100) And &HFF
b = (vbColor \ &H10000) And &HFF

VBColorToHTML = "#" & _
Right$("0" & Hex(r), 2) & _
Right$("0" & Hex(g), 2) & _
Right$("0" & Hex(b), 2)
End Function

Public Function HTMLToVBColor(htmlColor As String) As Long
Dim r As Long, g As Long, b As Long
If Left$(htmlColor, 1) = "#" Then htmlColor = Mid$(htmlColor, 2)

r = CLng("&H" & Mid$(htmlColor, 1, 2))
g = CLng("&H" & Mid$(htmlColor, 3, 2))
b = CLng("&H" & Mid$(htmlColor, 5, 2))

HTMLToVBColor = RGB(r, g, b)
End Function
End Class

' Example 3: Checksum calculator
Public Function CalculateChecksum(data() As Byte) As String
Dim checksum As Long
Dim i As Long

For i = LBound(data) To UBound(data)
checksum = checksum Xor data(i)
checksum = ((checksum And &H7FFFFFFF) * 2) Or (checksum And &H80000000) \ &H80000000
Next i

CalculateChecksum = Right$("00000000" & Hex(checksum), 8)
End Function

' Example 4: Binary file viewer
Public Class BinaryFileViewer
Public Function LoadAndDisplayFile(filePath As String) As String
Dim fileNum As Integer
Dim fileData() As Byte
Dim fileSize As Long
Dim result As String
Dim i As Long

fileNum = FreeFile
Open filePath For Binary As #fileNum
fileSize = LOF(fileNum)
ReDim fileData(0 To fileSize - 1)
Get #fileNum, , fileData
Close #fileNum

For i = 0 To UBound(fileData)
If i Mod 16 = 0 Then
result = result & vbCrLf & Right$("00000000" & Hex(i), 8) & ": "
End If
result = result & Right$("0" & Hex(fileData(i)), 2) & " "
Next i

LoadAndDisplayFile = result
End Function
End Class

Error Handling

The Hex function generally doesn't raise errors, but type conversion can:

On Error Resume Next
Dim result As String
result = Hex(someValue)
If Err.Number <> 0 Then
Debug.Print "Error converting to hex: " & Err.Description
Err.Clear
End If
On Error GoTo 0

Performance Considerations

Best Practices

  1. Padding: Use Right$() or Format$() to pad hex values to fixed width
  2. Prefixes: Add "&H" prefix when the value will be parsed back to numeric
  3. Case: VB6 Hex returns uppercase (A-F); convert to lowercase if needed
  4. Validation: Validate input before conversion to avoid type mismatch errors
  5. Documentation: Document whether hex strings include prefixes ("0x", "&H")
  6. Null Handling: Check for Null before calling Hex if working with Variants

Comparison with Other Functions

Function Purpose Example
Hex Decimal to hexadecimal string Hex(255) → "FF"
Oct Decimal to octal string Oct(8) → "10"
Str Number to decimal string Str(255) → " 255"
Format Number to formatted string Format(255, "00") → "255"
CLng("&H...") Hexadecimal string to Long CLng("&HFF") → 255

Platform and Version Notes

Limitations

← Back to Conversion | View all functions