<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - hex_dollar - Conversion">
<title>hex_dollar - Conversion - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/conversion/index.html">Conversion</a> / hex_dollar</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="hex-function">Hex$ Function</h1>
<p>The <code>Hex$</code> function in Visual Basic 6 returns a string representing the hexadecimal (base-16)
value of a number. The dollar sign (<code>$</code>) suffix indicates that this function always returns a
<code>String</code> type, never a <code>Variant</code>.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Hex$(number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>number</code> - Required. Any valid numeric expression or string expression. If <code>number</code> is not a
whole number, it is rounded to the nearest whole number before being evaluated.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> representing the hexadecimal value of <code>number</code>. The string contains only
hexadecimal digits (0-9, A-F) without any prefix (no "0x" or "&H").</p>
<h2 id="behavior-and-characteristics">Behavior and Characteristics</h2>
<h3 id="number-range-and-representation">Number Range and Representation</h3>
<ul>
<li>Positive numbers: Returns hexadecimal representation without leading zeros</li>
<li>Negative numbers: Returns two's complement representation</li>
<li><code>Byte</code> values: Up to 2 hex digits (00-FF)</li>
<li><code>Integer</code> values: Up to 4 hex digits (0000-FFFF)</li>
<li><code>Long</code> values: Up to 8 hex digits (00000000-FFFFFFFF)</li>
<li>Zero: Returns "0" (single character)</li>
<li>If <code>number</code> contains <code>Null</code>, returns <code>Null</code></li>
</ul>
<h3 id="type-differences-hex-vs-hex">Type Differences: <code>Hex$</code> vs <code>Hex</code></h3>
<ul>
<li><code>Hex$</code>: Always returns <code>String</code> type (never <code>Variant</code>)</li>
<li><code>Hex</code>: Returns <code>Variant</code> (can propagate <code>Null</code> values)</li>
<li>Use <code>Hex$</code> when you need guaranteed <code>String</code> return type</li>
<li>Use <code>Hex</code> when working with potentially <code>Null</code> values</li>
</ul>
<h3 id="formatting-characteristics">Formatting Characteristics</h3>
<ul>
<li>No "0x" or "&H" prefix in output</li>
<li>Uses uppercase letters (A-F, not a-f)</li>
<li>No leading zeros for positive numbers (except zero itself)</li>
<li>Negative numbers use two's complement representation</li>
<li>Maximum 8 characters for <code>Long</code> type</li>
</ul>
<h2 id="common-usage-patterns">Common Usage Patterns</h2>
<h3 id="1-convert-numbers-to-hex-strings">1. Convert Numbers to Hex Strings</h3>
<pre><code class="language-vbnet">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"</code></pre>
<h3 id="2-display-rgb-color-values">2. Display RGB Color Values</h3>
<pre><code class="language-vbnet">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))</code></pre>
<h3 id="3-debug-memory-addresses">3. Debug Memory Addresses</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="4-generate-unique-identifiers">4. Generate Unique Identifiers</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="5-format-byte-arrays-as-hex-strings">5. Format Byte Arrays as Hex Strings</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="6-log-error-codes-in-hex">6. Log Error Codes in Hex</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="7-convert-character-codes">7. Convert Character Codes</h3>
<pre><code class="language-vbnet">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"</code></pre>
<h3 id="8-create-hexadecimal-dump">8. Create Hexadecimal Dump</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="9-parse-and-format-checksums">9. Parse and Format Checksums</h3>
<pre><code class="language-vbnet">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)</code></pre>
<h3 id="10-network-protocol-debugging">10. Network Protocol Debugging</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Hex()</code> - Returns a <code>Variant</code> containing the hexadecimal value (can handle <code>Null</code>)</li>
<li><code>Oct$()</code> - Returns the octal (base-8) representation of a number</li>
<li><code>Str$()</code> - Converts a number to its decimal string representation</li>
<li><code>Val()</code> - Converts a string to a numeric value</li>
<li><code>CLng()</code> - Converts an expression to a <code>Long</code> integer</li>
<li><code>Asc()</code> - Returns the character code of the first character in a string</li>
<li><code>Chr$()</code> - Returns the character associated with a character code</li>
<li><code>Format$()</code> - Formats expressions with more control over output</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="padding-hex-values">Padding Hex Values</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="adding-hex-prefix">Adding Hex Prefix</h3>
<pre><code class="language-vbnet">Function HexWithPrefix(value As Long) As String
HexWithPrefix = "&H" & Hex$(value) ' VB6 style
' Or: HexWithPrefix = "0x" & Hex$(value) ' C style
End Function</code></pre>
<h3 id="converting-back-from-hex-string">Converting Back from Hex String</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="handling-byte-order-endianness">Handling Byte Order (Endianness)</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Hex$</code> is very fast for converting numbers to hexadecimal strings</li>
<li>No significant performance difference between <code>Hex</code> and <code>Hex$</code> for non-Null values</li>
<li>String concatenation in loops can be slow; consider building arrays and using <code>Join</code></li>
<li>For large byte arrays, consider buffering output</li>
</ul>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="common-pitfalls">Common Pitfalls</h2>
<h3 id="1-no-automatic-padding">1. No Automatic Padding</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="2-negative-numbers-use-twos-complement">2. Negative Numbers Use Two's Complement</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="3-no-prefix-in-output">3. No Prefix in Output</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="4-uppercase-output-only">4. Uppercase Output Only</h3>
<pre><code class="language-vbnet">' Hex$ always returns uppercase A-F
Debug.Print Hex$(255) ' "FF" (not "ff")
' Convert to lowercase if needed
hexValue = LCase$(Hex$(255)) ' "ff"</code></pre>
<h3 id="5-rounding-of-non-integer-values">5. Rounding of Non-Integer Values</h3>
<pre><code class="language-vbnet">' 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)</code></pre>
<h3 id="6-type-range-limitations">6. Type Range Limitations</h3>
<pre><code class="language-vbnet">' 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)</code></pre>
<h2 id="practical-examples">Practical Examples</h2>
<h3 id="memory-dump-utility">Memory Dump Utility</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="uuidguid-formatter">UUID/GUID Formatter</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="color-manipulation">Color Manipulation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Returns only uppercase hexadecimal letters (A-F), not lowercase</li>
<li>Does not include "&H" or "0x" prefix (must add manually)</li>
<li>Does not pad with leading zeros (must pad manually)</li>
<li>Cannot handle <code>Null</code> values (use <code>Hex</code> variant function instead)</li>
<li>Limited to 32-bit <code>Long</code> integer range (no 64-bit support in VB6)</li>
<li>Negative numbers return two's complement representation</li>
<li>Fractional values are rounded before conversion</li>
<li>No direct support for byte-order conversion (endianness)</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Conversion</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>