Visual Basic Language Reference  

Asc, AscW Functions

Returns an Integer value representing the character code corresponding to a character.

Public Overloads Function Asc(ByVal String As Char) As Integer
Public Overloads Function AscW(ByVal String As Char) As Integer

-or-

Public Overloads Function Asc(ByVal String As String) As Integer
Public Overloads Function AscW(ByVal String As String) As Integer

Parameter

String
Required. Any valid Char or String expression. If String is a String expression, only the first character of the string is used for input. If String is Nothing or contains no characters, an ArgumentException error occurs.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 String is not specified or is zero length.

Remarks

Asc returns the code point, or character code, for the input character. This can be 0 through 255 for single-byte character set (SBCS) values and -32768 through 32767 for double-byte character set (DBCS) values. The returned value depends on the code page for the current thread, which is contained in the ANSICodePage property of the TextInfo class. TextInfo.ANSICodePage can be obtained by specifying System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage.

AscW returns the Unicode code point for the input character. This can be 0 through 65535. The returned value is independent of the culture and code page settings for the current thread.

Example

This example uses the Asc function to return Integer character codes corresponding to the first letter in each string.

Dim MyInt As Integer
MyInt = Asc("A")   ' MyInt is set to 65.
MyInt = Asc("a")   ' MyInt is set to 97.
MyInt = Asc("Apple")   ' MyInt is set to 65.

See Also

Chr, ChrW Functions | Conversion Functions | Type Conversion Functions | System.Globalization Namespace | CultureInfo Class | TextInfo.ANSICodePage Property | ArgumentException