Dela via


Asc, AscW Functions

Returns an Integer value that represents 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

Parameters

  • 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

Exception type

Error number

Condition

ArgumentException

5

String is empty or is zero length.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when it is possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

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.

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.

Note

The AscB function of earlier versions of Visual Basic returns a code for a byte, instead of a character. It is used primarily for converting strings in double-byte character set (DBCS) applications. Beginning with Visual Basic 2005, strings are in Unicode, and AscB is no longer supported.

Example

The following example uses the Asc function to return Integer Data Type (Visual Basic) character codes corresponding to the first letter in each string.

Dim codeInt As Integer 
' The following line of code sets codeInt to 65.
codeInt = Asc("A")
' The following line of code sets codeInt to 97.
codeInt = Asc("a")
' The following line of code sets codeInt to 65.
codeInt = Asc("Apple")

Requirements

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

Chr, ChrW Functions

Conversion Functions (Visual Basic)

Type Conversion Functions

Integer Data Type (Visual Basic)

System.Globalization

CultureInfo

ANSICodePage

ArgumentException