VB6Parse / Library / Conversion / hex_dollar

VB6 Library Reference

Hex$ Function

The Hex$ function in Visual Basic 6 returns a string representing the hexadecimal (base-16) value of a number. The dollar sign ($) suffix indicates that this function always returns a String type, never a Variant.

Syntax

Hex$(number)

Parameters

Return Value

Returns a String representing the hexadecimal value of number. The string contains only hexadecimal digits (0-9, A-F) without any prefix (no "0x" or "&H").

Behavior and Characteristics

Number Range and Representation

Type Differences: Hex$ vs Hex

Formatting Characteristics

Common Usage Patterns

1. Convert Numbers to Hex Strings

Function NumberToHex(value As Long) As String
NumberToHex = Hex$(value)
End Function

Debug.Print NumberToHex(255)      ' "FF"
Debug.Print NumberToHex(4096)     ' "1000"
Debug.Print NumberToHex(65535)    ' "FFFF"

2. Display RGB Color Values

Function ColorToHex(colorValue As Long) As String
Dim hexStr As String
hexStr = Hex$(colorValue)
' Pad to 6 characters for web colors
ColorToHex = String$(6 - Len(hexStr), "0") & hexStr
End Function

Dim webColor As String
webColor = "#" & ColorToHex(RGB(255, 128, 64))

3. Debug Memory Addresses

Function FormatAddress(address As Long) As String
Dim hexAddr As String
hexAddr = Hex$(address)
' Pad to 8 characters
FormatAddress = "0x" & String$(8 - Len(hexAddr), "0") & hexAddr
End Function

4. Generate Unique Identifiers

Function GenerateHexID() As String
Randomize
Dim part1 As Long, part2 As Long
part1 = Int(Rnd * &H7FFFFFFF)
part2 = Int(Rnd * &H7FFFFFFF)
GenerateHexID = Hex$(part1) & Hex$(part2)
End Function

5. Format Byte Arrays as Hex Strings

Function BytesToHex(bytes() As Byte) As String
Dim result As String
Dim i As Integer
Dim hexByte As String

For i = LBound(bytes) To UBound(bytes)
hexByte = Hex$(bytes(i))
If Len(hexByte) = 1 Then hexByte = "0" & hexByte
result = result & hexByte
Next i

BytesToHex = result
End Function

6. Log Error Codes in Hex

Sub LogError(errNum As Long, errDesc As String)
Dim logFile As Integer
logFile = FreeFile
Open "errors.log" For Append As #logFile
Print #logFile, "Error 0x" & Hex$(errNum) & ": " & errDesc
Close #logFile
End Sub

7. Convert Character Codes

Function CharToHex(ch As String) As String
If Len(ch) > 0 Then
CharToHex = Hex$(Asc(ch))
Else
CharToHex = ""
End If
End Function

Debug.Print CharToHex("A")  ' "41"
Debug.Print CharToHex("Z")  ' "5A"

8. Create Hexadecimal Dump

Function HexDump(data As String, Optional bytesPerLine As Integer = 16) As String
Dim result As String
Dim i As Long
Dim hexVal As String

For i = 1 To Len(data)
hexVal = Hex$(Asc(Mid$(data, i, 1)))
If Len(hexVal) = 1 Then hexVal = "0" & hexVal
result = result & hexVal & " "

If (i Mod bytesPerLine) = 0 Then
result = result & vbCrLf
End If
Next i

HexDump = result
End Function

9. Parse and Format Checksums

Function FormatChecksum(checksum As Long) As String
Dim hexStr As String
hexStr = Hex$(checksum)
' Pad to 8 characters
FormatChecksum = String$(8 - Len(hexStr), "0") & hexStr
End Function

Dim crc32 As Long
crc32 = CalculateCRC32(fileData)
Debug.Print "CRC32: " & FormatChecksum(crc32)

10. Network Protocol Debugging

Function FormatPacketHeader(packetType As Byte, packetLen As Integer) As String
Dim typeHex As String, lenHex As String

typeHex = Hex$(packetType)
If Len(typeHex) = 1 Then typeHex = "0" & typeHex

lenHex = Hex$(packetLen)
While Len(lenHex) < 4
lenHex = "0" & lenHex
Wend

FormatPacketHeader = "Type: 0x" & typeHex & " Len: 0x" & lenHex
End Function

Best Practices

Padding Hex Values

' Pad to specific width for consistent formatting
Function PadHex(value As Long, width As Integer) As String
Dim hexStr As String
hexStr = Hex$(value)

If Len(hexStr) < width Then
PadHex = String$(width - Len(hexStr), "0") & hexStr
Else
PadHex = hexStr
End If
End Function

Debug.Print PadHex(255, 4)   ' "00FF"
Debug.Print PadHex(4096, 8)  ' "00001000"

Adding Hex Prefix

Function HexWithPrefix(value As Long) As String
HexWithPrefix = "&H" & Hex$(value)  ' VB6 style
' Or: HexWithPrefix = "0x" & Hex$(value)  ' C style
End Function

Converting Back from Hex String

Function HexToLong(hexStr As String) As Long
' Remove any prefix
If Left$(hexStr, 2) = "&H" Or Left$(hexStr, 2) = "0x" Then
hexStr = Mid$(hexStr, 3)
End If

' Convert using Val with &H prefix
HexToLong = Val("&H" & hexStr)
End Function

Handling Byte Order (Endianness)

Function LongToHexBytes(value As Long) As String
Dim b1 As Byte, b2 As Byte, b3 As Byte, b4 As Byte

b1 = value And &HFF
b2 = (value \ &H100) And &HFF
b3 = (value \ &H10000) And &HFF
b4 = (value \ &H1000000) And &HFF

' Little-endian format
LongToHexBytes = Right$("0" & Hex$(b1), 2) & " " & _
Right$("0" & Hex$(b2), 2) & " " & _
Right$("0" & Hex$(b3), 2) & " " & _
Right$("0" & Hex$(b4), 2)
End Function

Performance Considerations

' Efficient for large arrays
Function BytesToHexEfficient(bytes() As Byte) As String
Dim chunks() As String
ReDim chunks(UBound(bytes) - LBound(bytes))

Dim i As Long, idx As Long
For i = LBound(bytes) To UBound(bytes)
chunks(idx) = Right$("0" & Hex$(bytes(i)), 2)
idx = idx + 1
Next i

BytesToHexEfficient = Join(chunks, "")
End Function

Common Pitfalls

1. No Automatic Padding

' Hex$ does NOT add leading zeros
Debug.Print Hex$(15)    ' "F" (not "0F")
Debug.Print Hex$(255)   ' "FF" (correct)
Debug.Print Hex$(16)    ' "10" (not "0010")

' Must pad manually for consistent width
Function PadHex(val As Integer) As String
Dim h As String
h = Hex$(val)
PadHex = String$(4 - Len(h), "0") & h
End Function

2. Negative Numbers Use Two's Complement

' Negative numbers are represented in two's complement
Debug.Print Hex$(-1)     ' "FFFFFFFF" (Long)
Debug.Print Hex$(-256)   ' "FFFFFF00"

' For signed interpretation, check range
Function SignedHex(value As Long) As String
If value < 0 Then
SignedHex = "-&H" & Hex$(Abs(value))
Else
SignedHex = "&H" & Hex$(value)
End If
End Function

3. No Prefix in Output

' Hex$ does NOT include "&H" or "0x" prefix
Dim hexValue As String
hexValue = Hex$(255)      ' "FF" (not "&HFF" or "0xFF")

' Add prefix manually if needed
hexValue = "&H" & Hex$(255)  ' "&HFF"
hexValue = "0x" & Hex$(255)  ' "0xFF"

4. Uppercase Output Only

' Hex$ always returns uppercase A-F
Debug.Print Hex$(255)  ' "FF" (not "ff")

' Convert to lowercase if needed
hexValue = LCase$(Hex$(255))  ' "ff"

5. Rounding of Non-Integer Values

' Non-integers are rounded before conversion
Debug.Print Hex$(15.3)   ' "F" (15 rounded)
Debug.Print Hex$(15.7)   ' "10" (16 rounded)
Debug.Print Hex$(15.5)   ' "10" (banker's rounding to even)

' Use Fix or Int if you need specific rounding
Debug.Print Hex$(Int(15.7))   ' "F" (truncated to 15)
Debug.Print Hex$(Fix(15.7))   ' "F" (truncated to 15)

6. Type Range Limitations

' Different types have different ranges
Dim b As Byte
Dim i As Integer
Dim l As Long

b = 255
Debug.Print Hex$(b)  ' "FF"

i = -1
Debug.Print Hex$(i)  ' "FFFF" (16-bit two's complement)

l = -1
Debug.Print Hex$(l)  ' "FFFFFFFF" (32-bit two's complement)

Practical Examples

Memory Dump Utility

Sub DumpMemory(startAddr As Long, length As Integer)
Dim i As Integer
Dim addr As Long
Dim byteVal As Byte
Dim line As String
Dim ascii As String

For i = 0 To length - 1
If (i Mod 16) = 0 Then
If i > 0 Then
Debug.Print line & "  " & ascii
End If
addr = startAddr + i
line = Right$("00000000" & Hex$(addr), 8) & ": "
ascii = ""
End If

' Get byte value (pseudo-code)
byteVal = GetMemoryByte(startAddr + i)
line = line & Right$("0" & Hex$(byteVal), 2) & " "

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

' Print last line
If ascii <> "" Then
Debug.Print line & String$(3 * (16 - Len(ascii)), " ") & "  " & ascii
End If
End Sub

UUID/GUID Formatter

Function FormatGUID(data1 As Long, data2 As Integer, data3 As Integer, _
data4() As Byte) As String
Dim result As String
Dim i As Integer

result = Right$("00000000" & Hex$(data1), 8) & "-"
result = result & Right$("0000" & Hex$(data2), 4) & "-"
result = result & Right$("0000" & Hex$(data3), 4) & "-"

For i = 0 To 1
result = result & Right$("0" & Hex$(data4(i)), 2)
Next i
result = result & "-"

For i = 2 To 7
result = result & Right$("0" & Hex$(data4(i)), 2)
Next i

FormatGUID = result
End Function

Color Manipulation

Function RGBToWebColor(r As Byte, g As Byte, b As Byte) As String
RGBToWebColor = "#" & _
Right$("0" & Hex$(r), 2) & _
Right$("0" & Hex$(g), 2) & _
Right$("0" & Hex$(b), 2)
End Function

Function WebColorToRGB(webColor As String, r As Byte, g As Byte, b As Byte)
' Remove # if present
If Left$(webColor, 1) = "#" Then webColor = Mid$(webColor, 2)

r = Val("&H" & Mid$(webColor, 1, 2))
g = Val("&H" & Mid$(webColor, 3, 2))
b = Val("&H" & Mid$(webColor, 5, 2))
End Function

Limitations

← Back to Conversion | View all functions