VB6Parse / Library / String / chrw

VB6 Library Reference

ChrW Function

Returns a String containing the Unicode character associated with the specified character code. The "W" suffix indicates this is the wide (Unicode) version of the Chr function.

Syntax

ChrW(charcode)

Parameters

Returns

Returns a String containing a single Unicode character corresponding to the specified code.

Remarks

Typical Uses

  1. International text support - Create strings with characters from various languages
  2. Unicode symbol insertion - Insert mathematical symbols, currency symbols, arrows, etc.
  3. XML/HTML entity handling - Convert numeric character references to actual characters
  4. Special character creation - Generate Unicode control characters and formatting marks
  5. Cross-platform text - Ensure consistent character representation across different systems
  6. Unicode file processing - Read and write Unicode text files correctly
  7. Internationalization (i18n) - Support multiple languages in applications

Basic Examples

' Example 1: Simple Unicode character
Dim ch As String
ch = ChrW(65)  ' Returns "A" (same as Chr for ASCII range)
' Example 2: Euro symbol
Dim euro As String
euro = ChrW(8364)  ' Returns "€"
' Example 3: Greek letter
Dim alpha As String
alpha = ChrW(945)  ' Returns "α" (Greek small letter alpha)
' Example 4: Chinese character
Dim hanzi As String
hanzi = ChrW(20013)  ' Returns "中" (Chinese character for "middle")

Common Patterns

Unicode Line Separator

Const UNICODE_LINE_SEP = 8232
text = "Line 1" & ChrW(UNICODE_LINE_SEP) & "Line 2"

Bullet Point List

Dim bullet As String
bullet = ChrW(8226)  ' "•"
list = bullet & " Item 1" & vbCrLf & bullet & " Item 2"
Dim copyright As String
copyright = ChrW(169)  ' "©"
notice = "Copyright " & copyright & " 2025"

Mathematical Symbols

Dim infinity As String, pi As String
infinity = ChrW(8734)  ' "∞"
pi = ChrW(960)  ' "π"
equation = pi & " " & ChrW(8776) & " 3.14"  ' "π ≈ 3.14"

Arrow Symbols

Dim rightArrow As String
rightArrow = ChrW(8594)  ' "→"
leftArrow = ChrW(8592)   ' "←"

Non-Breaking Space

Dim nbsp As String
nbsp = ChrW(160)  ' Non-breaking space
text = "Price:" & nbsp & "$100"

Emoji and Symbols (BMP only)

Dim heart As String, star As String
heart = ChrW(9829)  ' "♥"
star = ChrW(9733)   ' "★"

Zero-Width Characters

Dim zwj As String
zwj = ChrW(8205)  ' Zero-width joiner

Currency Symbols

Dim pound As String, yen As String
pound = ChrW(163)  ' "£"
yen = ChrW(165)    ' "¥"

Diacritical Marks

Dim acute As String
acute = ChrW(180)  ' "´" (acute accent)
combined = "e" & ChrW(769)  ' Combining acute accent

Advanced Examples

Building Multilingual Text

Function GetGreeting(language As String) As String
Select Case language
Case "chinese"
GetGreeting = ChrW(20320) & ChrW(22909)  ' "你好"
Case "japanese"
GetGreeting = ChrW(12371) & ChrW(12435) & ChrW(12395) & ChrW(12385) & ChrW(12399)  ' "こんにちは"
Case "korean"
GetGreeting = ChrW(50504) & ChrW(45397) & ChrW(54616) & ChrW(49464) & ChrW(50836)  ' "안녕하세요"
Case "russian"
GetGreeting = ChrW(1055) & ChrW(1088) & ChrW(1080) & ChrW(1074) & ChrW(1077) & ChrW(1090)  ' "Привет"
Case Else
GetGreeting = "Hello"
End Select
End Function

HTML Entity Decoder

Function DecodeNumericEntity(entity As String) As String
' Decode &#nnnn; or &#xhhhh; entities
Dim code As Long

If InStr(entity, "&#x") > 0 Then
' Hexadecimal entity
code = CLng("&H" & Mid(entity, 4, Len(entity) - 4))
ElseIf InStr(entity, "&#") > 0 Then
' Decimal entity
code = CLng(Mid(entity, 3, Len(entity) - 3))
End If

If code >= 0 And code <= 65535 Then
DecodeNumericEntity = ChrW(code)
End If
End Function

Unicode Range Validator

Function IsInUnicodeRange(char As String, rangeStart As Long, rangeEnd As Long) As Boolean
If Len(char) = 0 Then Exit Function

Dim code As Long
code = AscW(char)

IsInUnicodeRange = (code >= rangeStart And code <= rangeEnd)
End Function

' Example usage:
' If IsInUnicodeRange(char, 0x4E00, 0x9FFF) Then
'     Debug.Print "CJK Unified Ideograph"
' End If

Unicode Text File Writer

Sub WriteUnicodeFile(filename As String, text As String)
Dim fso As Object, stream As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = CreateObject("ADODB.Stream")

stream.Type = 2  ' Text
stream.Charset = "UTF-8"
stream.Open

' Add BOM for UTF-8
stream.WriteText ChrW(65279)  ' UTF-8 BOM
stream.WriteText text

stream.SaveToFile filename, 2
stream.Close
End Sub

Error Handling

Function SafeChrW(charcode As Long) As String
On Error GoTo ErrorHandler

' Normalize negative values
If charcode < 0 Then
charcode = 65536 + charcode
End If

If charcode >= 0 And charcode <= 65535 Then
SafeChrW = ChrW(charcode)
Else
SafeChrW = "?"  ' Replacement character for invalid codes
End If
Exit Function

ErrorHandler:
SafeChrW = "?"
End Function

Performance Notes

Best Practices

  1. Use ChrW for Unicode - Always use ChrW instead of Chr when working with international text
  2. Validate ranges - Check that character codes are within valid Unicode ranges
  3. Handle errors - Wrap ChrW calls in error handlers when processing user input
  4. Document character codes - Use constants or comments to explain non-obvious character codes
  5. Test with actual data - Verify Unicode text displays correctly in target environments
  6. Consider normalization - Be aware that some characters can be represented multiple ways
  7. Use UTF-8 for files - When saving Unicode text, prefer UTF-8 encoding
Function Character Set Range Use Case
Chr System default (ANSI/Unicode) 0-255 Legacy code, simple ASCII
ChrB ANSI (byte) 0-255 ANSI text, byte operations
ChrW Unicode (wide) 0-65535 International text, symbols
AscW Unicode (inverse) Returns 0-65535 Get Unicode code from character

Unicode Ranges Reference

Some common Unicode ranges that work with ChrW:

Platform Notes

Limitations

← Back to String | View all functions