ChrB$ Function
Returns a String containing the character associated with the specified ANSI character code.
The dollar sign suffix ($) explicitly indicates that this function returns a String type
(not a Variant), and the "B" suffix indicates this is the byte (ANSI) version.
Syntax
ChrB$(charcode)
Parameters
charcode: Required.Longvalue that identifies a character in the ANSI character set. Valid values are 0-255. For values outside this range, an error occurs.
Return Value
Returns a String containing the single byte character corresponding to the specified ANSI code.
The return value is always a String type (never Variant), and represents a single-byte
character from the ANSI character set.
Remarks
- The
ChrB$function combines the behavior ofChrB(byte character) with the$suffix (explicitStringreturn type). - Valid range: 0-255 (Error 5 "Invalid procedure call or argument" for values outside range).
ChrB$(0)returns a null character.ChrB$(13)returns carriage return (vbCr).ChrB$(10)returns line feed (vbLf).ChrB$(9)returns tab character (vbTab).- Values 0-31 are non-printable control characters.
- Values 32-126 are standard printable ASCII characters.
- Values 127-255 depend on the system code page (often Windows-1252 in VB6).
- The inverse function is
AscB, which returns the numeric byte value of a character. - For better performance when you know the result is a string, use
ChrB$instead ofChrB.
Common Character Codes
| Code | Character | Constant | Description |
|---|---|---|---|
| 0 | (null) | vbNullChar |
Null character |
| 9 | \t | vbTab |
Horizontal tab |
| 10 | \n | vbLf |
Line feed |
| 13 | \r | vbCr |
Carriage return |
| 32 | (space) | - | Space character |
| 34 | " | - | Double quote |
| 39 | ' | - | Single quote |
| 65 | A | - | Uppercase A |
| 97 | a | - | Lowercase a |
Typical Uses
- Building ANSI strings - Construct strings from byte values
- Line breaks - Insert carriage returns and line feeds
- Special characters - Add tabs, quotes, and other special characters
- Byte-level operations - Work with binary data or legacy file formats
- ANSI text generation - Create strings for systems expecting ANSI encoding
- Legacy protocol support - Work with older communication protocols
- Control characters - Generate non-printable control characters
Basic Examples
' Example 1: Get character from code
Dim ch As String
ch = ChrB$(65) ' Returns "A"
' Example 2: Lowercase letter
Dim lower As String
lower = ChrB$(97) ' Returns "a"
' Example 3: Special character
Dim space As String
space = ChrB$(32) ' Returns " "
' Example 4: Line break
Dim msg As String
msg = "Line 1" & ChrB$(13) & ChrB$(10) & "Line 2"
Common Patterns
Multi-line Strings
Function CreateMultiLine() As String
Dim result As String
result = "First Line" & ChrB$(13) & ChrB$(10)
result = result & "Second Line" & ChrB$(13) & ChrB$(10)
result = result & "Third Line"
CreateMultiLine = result
End Function
Tab-Separated Values
Function CreateTSV(col1 As String, col2 As String, col3 As String) As String
CreateTSV = col1 & ChrB$(9) & col2 & ChrB$(9) & col3
End Function
Build String from Byte Array
Function BytesToString(bytes() As Byte) As String
Dim i As Integer
Dim result As String
For i = LBound(bytes) To UBound(bytes)
result = result & ChrB$(bytes(i))
Next i
BytesToString = result
End Function
Quote in String
Function AddQuotes(text As String) As String
AddQuotes = ChrB$(34) & text & ChrB$(34)
End Function
Null-Terminated String
Function CreateNullTerminated(text As String) As String
CreateNullTerminated = text & ChrB$(0)
End Function
ANSI Protocol Message
Function CreateProtocolMessage(msgType As Byte, data As String) As String
Dim msg As String
' SOH (Start of Header)
msg = ChrB$(1)
' Message type
msg = msg & ChrB$(msgType)
' STX (Start of Text)
msg = msg & ChrB$(2)
' Payload
msg = msg & data
' ETX (End of Text)
msg = msg & ChrB$(3)
CreateProtocolMessage = msg
End Function
Generate Alphabet
Function GenerateAlphabet() As String
Dim i As Integer
Dim result As String
For i = 65 To 90
result = result & ChrB$(i)
Next i
GenerateAlphabet = result ' Returns "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
End Function
CSV Field with Quotes
Function QuoteCSVField(field As String) As String
Dim quoted As String
' Replace " with ""
quoted = Replace(field, ChrB$(34), ChrB$(34) & ChrB$(34))
QuoteCSVField = ChrB$(34) & quoted & ChrB$(34)
End Function
Password Mask
Function MaskPassword(length As Integer) As String
Dim i As Integer
Dim result As String
For i = 1 To length
result = result & ChrB$(42) ' Asterisk
Next i
MaskPassword = result
End Function
Character Range
Function GetPrintableChars() As String
Dim i As Integer
Dim result As String
For i = 32 To 126
result = result & ChrB$(i)
Next i
GetPrintableChars = result
End Function
Related Functions
ChrB: Returns byte character asVariantinstead ofStringChr$: Returns ANSI/Unicode character (system dependent)ChrW$: Returns Unicode character (2 bytes)AscB: Returns byte value of first byte in string (inverse ofChrB$)AscB$: Not a valid function (there is noAscB$)
Error Handling
Function SafeChrB(code As Long) As String
On Error Resume Next
SafeChrB = ChrB$(code)
If Err.Number <> 0 Then
SafeChrB = ""
Err.Clear
End If
End Function
Performance Considerations
ChrB$is slightly more efficient thanChrBbecause it avoidsVariantoverhead- For building strings from many bytes, consider using a buffer or byte array
- Concatenating many
ChrB$calls can be slow; use arrays andJoinfor better performance - When working with large amounts of byte data, consider
Stringfunction or byte arrays
Best Practices
- Use named constants for common control characters instead of magic numbers
- Validate character codes are in the range 0-255 before calling
ChrB$ - Use
ChrB$for ANSI/byte operations,ChrW$for Unicode operations - Document when using non-printable characters (codes 0-31)
- Consider code page issues when working with extended ANSI (128-255)
- Use
vbCrLfconstant instead ofChrB$(13) & ChrB$(10)when possible - Prefer
ChrB$overChrBwhen you need aStringresult
Limitations
- Limited to character codes 0-255 (use
ChrW$for full Unicode support) - Character interpretation depends on system code page for values 128-255
- Does not validate that the resulting character is printable
- No direct support for multi-byte Unicode characters