VB6Parse / Library / String / asc

VB6 Library Reference

Asc Function

Returns an Integer representing the character code corresponding to the first letter in a string.

Syntax

Asc(string)

Parameters

Return Value

Returns an Integer representing the ANSI character code of the first character in the string. - For ANSI characters (0-127), returns standard ASCII values - For extended ANSI characters (128-255), returns extended ASCII values - Unicode characters are converted to ANSI before the code is returned

Remarks

The Asc function returns the numeric ANSI character code for the first character in a string. This is useful for: - Validating input characters - Performing character-based operations - Converting characters to their numeric representations - Character range checking

Important Notes

  1. Only First Character: Only the first character of the string is examined
  2. Empty String Error: Passing an empty string results in a run-time error (Error 5: Invalid procedure call or argument)
  3. ANSI vs. Unicode: In VB6, Asc returns ANSI codes; AscW returns Unicode values
  4. Return Type: Returns Integer (16-bit signed), range -32,768 to 32,767, but character codes are 0-255
  5. Case Sensitive: Upper and lowercase letters have different codes (e.g., "A" = 65, "a" = 97)

Character Code Ranges

Examples

Basic Usage

Dim code As Integer
code = Asc("A")          ' Returns 65
code = Asc("Apple")      ' Returns 65 (first character only)
code = Asc("a")          ' Returns 97
code = Asc("0")          ' Returns 48
code = Asc(" ")          ' Returns 32 (space)

Character Validation

Function IsDigit(ch As String) As Boolean
    Dim code As Integer
    code = Asc(ch)
    IsDigit = (code >= 48 And code <= 57)
End Function
Function IsUpperCase(ch As String) As Boolean
    Dim code As Integer
    code = Asc(ch)
    IsUpperCase = (code >= 65 And code <= 90)
End Function

Case Conversion Offset

' Calculate offset between upper and lower case
Dim offset As Integer
offset = Asc("a") - Asc("A")  ' Returns 32

Character Range Checking

Function IsPrintable(ch As String) As Boolean
    Dim code As Integer
    code = Asc(ch)
    IsPrintable = (code >= 32 And code <= 126)
End Function

String Encoding

Function EncodeString(s As String) As String
    Dim i As Integer
    Dim result As String
    For i = 1 To Len(s)
        If result <> "" Then result = result & ","
        result = result & CStr(Asc(Mid(s, i, 1)))
    Next i
    EncodeString = result
End Function

Common Patterns

1. Input Validation

If Asc(userInput) >= 48 And Asc(userInput) <= 57 Then
    ' First character is a digit
End If

2. Alphabetic Checking

Dim code As Integer
code = Asc(UCase(letter))
If code >= 65 And code <= 90 Then
    ' It's a letter
End If

3. CSV Parsing Helper

If Asc(field) = 34 Then  ' 34 is double quote
    ' Handle quoted field
End If

4. Character Comparison

If Asc(char1) < Asc(char2) Then
    ' char1 comes before char2 in ASCII order
End If

5. Special Character Detection

Select Case Asc(ch)
    Case 9      ' Tab
    Case 10     ' Line feed
    Case 13     ' Carriage return
    Case 32     ' Space
End Select

6. Keyboard Input Processing

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then  ' Enter key
        ' Process input
    End If
End Sub

7. Character Class Testing

Function IsControl(ch As String) As Boolean
    Dim code As Integer
    code = Asc(ch)
    IsControl = (code < 32 Or code = 127)
End Function

8. Simple Encryption

Function ROT13Char(ch As String) As String
    Dim code As Integer
    code = Asc(UCase(ch))
    If code >= 65 And code <= 90 Then
        code = ((code - 65 + 13) Mod 26) + 65
        ROT13Char = Chr(code)
    Else
        ROT13Char = ch
    End If
End Function

Common Character Codes

Character Code Description
Null 0 Null character
Tab 9 Horizontal tab
LF 10 Line feed
CR 13 Carriage return
Space 32 Space
! 33 Exclamation mark
" 34 Double quote
0 48 Digit zero
9 57 Digit nine
A 65 Uppercase A
Z 90 Uppercase Z
a 97 Lowercase a
z 122 Lowercase z
DEL 127 Delete

Error Handling

On Error Resume Next
code = Asc(inputString)
If Err.Number = 5 Then
    ' Empty string error
    MsgBox "String cannot be empty"
End If

Performance Notes

Parsing Notes

The Asc function is not a reserved keyword in VB6. It is parsed as a regular function call (CallExpression). This module exists primarily for documentation purposes and to provide a comprehensive test suite that validates the parser correctly handles Asc function calls in various contexts.

← Back to String | View all functions