VB6Parse / Library / String / chrb_dollar

VB6 Library Reference

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

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

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

  1. Building ANSI strings - Construct strings from byte values
  2. Line breaks - Insert carriage returns and line feeds
  3. Special characters - Add tabs, quotes, and other special characters
  4. Byte-level operations - Work with binary data or legacy file formats
  5. ANSI text generation - Create strings for systems expecting ANSI encoding
  6. Legacy protocol support - Work with older communication protocols
  7. 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

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

Best Practices

  1. Use named constants for common control characters instead of magic numbers
  2. Validate character codes are in the range 0-255 before calling ChrB$
  3. Use ChrB$ for ANSI/byte operations, ChrW$ for Unicode operations
  4. Document when using non-printable characters (codes 0-31)
  5. Consider code page issues when working with extended ANSI (128-255)
  6. Use vbCrLf constant instead of ChrB$(13) & ChrB$(10) when possible
  7. Prefer ChrB$ over ChrB when you need a String result

Limitations

← Back to String | View all functions