VB6Parse / Library / String / ascw

VB6 Library Reference

AscW Function

Returns an Integer representing the Unicode character code of the first character in a string. The "W" suffix indicates this is the wide (Unicode) version of the Asc function.

Syntax

AscW(string)

Parameters

Returns

Returns an Integer representing the Unicode code point (0-65535) of the first character in the string.

Remarks

Typical Uses

  1. International text processing - Work with characters from various languages
  2. Unicode character analysis - Examine Unicode code points in strings
  3. Character validation - Validate characters are in expected Unicode ranges
  4. Text encoding operations - Convert between different character encodings
  5. Symbol detection - Identify mathematical symbols, currency, arrows, etc.
  6. Character range checking - Determine if characters belong to specific scripts
  7. Multilingual sorting - Implement custom sort orders based on Unicode values

Basic Examples

' Example 1: Simple ASCII character
Dim code As Integer
code = AscW("A")  ' Returns 65
' Example 2: Euro symbol
Dim euroCode As Integer
euroCode = AscW("€")  ' Returns 8364
' Example 3: Greek letter
Dim alphaCode As Integer
alphaCode = AscW("α")  ' Returns 945
' Example 4: Chinese character
Dim hanziCode As Integer
hanziCode = AscW("中")  ' Returns 20013

Common Patterns

Check Unicode Range

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

Detect Character Script

Function GetCharacterScript(char As String) As String
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    Select Case code
        Case 0 To 127
            GetCharacterScript = "ASCII"
        Case 880 To 1023
            GetCharacterScript = "Greek"
        Case 1024 To 1279
            GetCharacterScript = "Cyrillic"
        Case 1424 To 1535
            GetCharacterScript = "Hebrew"
        Case 1536 To 1791
            GetCharacterScript = "Arabic"
        Case 19968 To 40959
            GetCharacterScript = "CJK"
        Case 44032 To 55203
            GetCharacterScript = "Hangul"
        Case Else
            GetCharacterScript = "Other"
    End Select
End Function

Validate Latin Characters

Function IsLatinChar(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Basic Latin + Latin-1 Supplement + Latin Extended-A and B
    IsLatinChar = (code >= 0 And code <= 591)
End Function

Check for Symbol Characters

Function IsSymbol(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Common symbol ranges
    IsSymbol = (code >= 8192 And code <= 8303) Or _
               (code >= 8352 And code <= 8399) Or _
               (code >= 8448 And code <= 8527) Or _
               (code >= 8704 And code <= 8959) Or _
               (code >= 9632 And code <= 9727)
End Function

Compare Unicode Values

Function CompareUnicode(char1 As String, char2 As String) As Integer
    If Len(char1) = 0 Or Len(char2) = 0 Then Exit Function
    CompareUnicode = AscW(char1) - AscW(char2)
End Function

Detect Emoji (BMP only)

Function IsEmojiBMP(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Emoticons and Miscellaneous Symbols
    IsEmojiBMP = (code >= 9728 And code <= 9983) Or _
                 (code >= 10084 And code <= 10084) Or _
                 (code >= 127744 And code <= 128511)
End Function

Extract Unicode Array

Function GetUnicodeArray(text As String) As Variant
    Dim codes() As Long
    Dim i As Long
    If Len(text) = 0 Then Exit Function
    ReDim codes(1 To Len(text))
    For i = 1 To Len(text)
        codes(i) = AscW(Mid(text, i, 1))
    Next i
    GetUnicodeArray = codes
End Function

Check Diacritical Marks

Function IsDiacriticalMark(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Combining Diacritical Marks
    IsDiacriticalMark = (code >= 768 And code <= 879)
End Function

Validate Email Characters

Function IsValidEmailChar(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Alphanumeric, dot, hyphen, underscore, @
    IsValidEmailChar = (code >= 48 And code <= 57) Or _
                       (code >= 65 And code <= 90) Or _
                       (code >= 97 And code <= 122) Or _
                       code = 45 Or code = 46 Or code = 64 Or code = 95
End Function

Detect Control Characters

Function IsUnicodeControl(char As String) As Boolean
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' C0 and C1 control characters
    IsUnicodeControl = (code >= 0 And code <= 31) Or _
                       (code >= 127 And code <= 159)
End Function

Advanced Examples

Unicode Normalization Check

Function CompareNormalized(str1 As String, str2 As String) As Boolean
    ' Simple comparison ignoring case
    If Len(str1) <> Len(str2) Then
        CompareNormalized = False
        Exit Function
    End If
    Dim i As Long
    Dim code1 As Long, code2 As Long
    For i = 1 To Len(str1)
        code1 = AscW(Mid(str1, i, 1))
        code2 = AscW(Mid(str2, i, 1))
        ' Convert to lowercase if uppercase Latin
        If code1 >= 65 And code1 <= 90 Then code1 = code1 + 32
        If code2 >= 65 And code2 <= 90 Then code2 = code2 + 32
        If code1 <> code2 Then
            CompareNormalized = False
            Exit Function
        End If
    Next i
    CompareNormalized = True
End Function

Unicode to HTML Entity

Function UnicodeToHTMLEntity(char As String) As String
    If Len(char) = 0 Then Exit Function
    Dim code As Long
    code = AscW(char)
    ' Create numeric entity
    UnicodeToHTMLEntity = "&#" & code & ";"
End Function

Multilingual Text Analyzer

Function AnalyzeText(text As String) As String
    Dim i As Long
    Dim code As Long
    Dim latinCount As Long
    Dim cjkCount As Long
    Dim arabicCount As Long
    Dim otherCount As Long
    For i = 1 To Len(text)
        code = AscW(Mid(text, i, 1))
        Select Case code
            Case 0 To 591
                latinCount = latinCount + 1
            Case 19968 To 40959
                cjkCount = cjkCount + 1
            Case 1536 To 1791
                arabicCount = arabicCount + 1
            Case Else
                otherCount = otherCount + 1
        End Select
    Next i
    AnalyzeText = "Latin: " & latinCount & ", CJK: " & cjkCount & _
                  ", Arabic: " & arabicCount & ", Other: " & otherCount
End Function

Character Category Validator

Function ValidateCategory(text As String, category As String) As Boolean
    Dim i As Long
    Dim code As Long
    For i = 1 To Len(text)
        code = AscW(Mid(text, i, 1))
        Select Case category
            Case "Digit"
                If Not (code >= 48 And code <= 57) Then
                    ValidateCategory = False
                    Exit Function
                End If
            Case "UpperLatin"
                If Not (code >= 65 And code <= 90) Then
                    ValidateCategory = False
                    Exit Function
                End If
            Case "LowerLatin"
                If Not (code >= 97 And code <= 122) Then
                    ValidateCategory = False
                    Exit Function
                End If
            Case Else
                ValidateCategory = False
                Exit Function
        End Select
    Next i
    ValidateCategory = True
End Function

Error Handling

Function SafeAscW(text As String) As Long
    On Error GoTo ErrorHandler
    If Len(text) = 0 Then
        SafeAscW = -1  ' Error indicator
        Exit Function
    End If
    SafeAscW = AscW(text)
    Exit Function
ErrorHandler:
    SafeAscW = -1
End Function

Performance Notes

Best Practices

  1. Validate input - Always check for empty strings before calling AscW
  2. Use for Unicode - Prefer AscW over Asc when working with international text
  3. Handle errors - Wrap AscW calls in error handlers when processing untrusted input
  4. Document ranges - Use constants or comments to explain Unicode range checks
  5. Consider normalization - Be aware that some characters have multiple Unicode representations
  6. Use with ChrW - Pair with ChrW for Unicode code point conversions
  7. Test edge cases - Verify behavior with empty strings, control characters, and non-BMP characters
Function Returns Character Set Use Case
Asc Integer (0-255 or Unicode) System default General character codes
AscB Integer (0-255) ANSI byte value Byte-level operations
AscW Integer (0-65535) Unicode code point International text
ChrW String (Unicode) Unicode (inverse) Convert code to character

Unicode Ranges Reference

Common Unicode ranges that can be detected with AscW: - Basic Latin (ASCII): 0-127 - Latin-1 Supplement: 128-255 - Latin Extended-A: 256-383 - Greek and Coptic: 880-1023 - Cyrillic: 1024-1279 - Hebrew: 1424-1535 - Arabic: 1536-1791 - Devanagari: 2304-2431 - Thai: 3584-3711 - Tibetan: 3840-4095 - CJK Unified Ideographs: 19968-40959 - Hangul Syllables: 44032-55203 - Currency Symbols: 8352-8399 - Mathematical Operators: 8704-8959 - Arrows: 8592-8703 - Box Drawing: 9472-9599 - Emoticons: 9728-9983

Platform Notes

Limitations

← Back to String | View all functions