Asc, AscB, AscW Functions

Named Arguments

No

Syntax

Asc(string)
AscB(string)
AscW(string)


string

Use: Required

Data Type: String

Any expression that evaluates to a string.

Return Value

An integer that represents the character code of the first character of the string. The range for the returned value is 0–255 on non-DBCS systems, but –32768–32767 on DBCS systems.

Description

Returns the ANSI or Unicode character code that represents the first character of the string passed to it. All other characters in the string are ignored. Use AscB with Byte data and AscW on Unicode (DBCS) systems.

Rules at a Glance

  • The string expression passed to the function must contain at least one character, or a runtime error (either "Invalid use of Null" or "Invalid procedure call or argument") is generated.

  • Only the first character of the string is evaluated by Asc, AscB, and AscW.

  • Use the AscW function to return the Unicode character of the first character of a string.

  • Use the AscB function to return the first byte of a string containing byte data.

Example

Dim sChars As String
Dim iCharCode As Integer
    
sChars = TextBox1.Text
If Len(sChars) > 0 Then
   iCharCode = Asc(sChars)
   If iCharCode >= 97 And iChar <= 122 Then
      MsgBox "The first character must be uppercase"
   End If
End If

Programming Tips and Gotchas

  • Always check that the string you are passing to the function contains at least one character using the Len function, as the following example shows:

    If Len(sMyString) > 0 Then
        iCharCode = Asc(sMyString)
    Else
        MsgBox "Cannot process a zero-length string"
    End If

  • On platforms which don't support Unicode, the AscW function performs exactly the same as Asc.

  • Surprisingly, although the VB Object Browser clearly shows that the data type of the parameter passed to the Asc function is String, it can actually be any data type. Evidently the Asc routine converts incoming values to strings before extracting their first character.

  • Use Asc within your data validation routines to determine such conditions as whether the first character is upper- or lowercase and whether it's alphabetic or numeric, as the following example demonstrates:

    Private Sub CommandButton1_Click()
    
    Dim sTest As String
    Dim iChar As Integer
        
    sTest = TextBox1.Text
    
    If Len(sTest) > 0 Then
       iChar = Asc(sTest)
       If iChar >= 65 And iChar <= 90 Then
          MsgBox "The first character is UPPERCASE"
       ElseIf iChar >= 97 And iChar <= 122 Then
          MsgBox "The first character is lowercase"
       Else
          MsgBox "The first character isn't alphabetical"
       End If
    Else
       MsgBox "Please enter something in the text box"
    End If
        
    End Sub

  • Use the Asc function and its converse, Chr, to create rudimentary encryption methods. Once you have obtained the character code for a particular character, you can perform calculations on this code to come up with a different number and then convert this to a character using the Chr function. To decrypt your string, simply reverse the calculation. Be sure, though, that your calculation doesn't generate character codes less than 20, since these are special nonprinting characters, which, if displayed or printed, can cause undesirable effects.

    Private Sub CommandButton2_Click()
    
    Dim MyName As String, MyEncryptedString As String
    Dim MyDecryptedString As String
    Dim i As Integer
    
    MyName = "Paul Lomax"
    
    For i = 1 To Len(MyName)
       MyEncryptedString = MyEncryptedString & _
                           Chr(Asc(Mid(MyName, i, 1)) + 25)
    Next i
    
    MsgBox "Hello, my name is " & MyEncryptedString
    
    For i = 1 To Len(MyName)
       MyDecryptedString = MyDecryptedString & _
       Chr(Asc(Mid(MyEncryptedString, i, 1)) - 25)
    Next i
    
    MsgBox "Hello, my name is " & MyDecryptedString
    
    End Sub

Sidebar 3. Unicode Characters and AscB, AscW, ChrB, and ChrW

The Unicode character set was developed to support software internationalization and the consequent need for many more characters than the original ASCII character set could provide. Unicode characters are represented by two bytes and can therefore represent up to 65,536 characters, whereas ANSI's one-byte representation can cope only with 256. Today, both Windows NT and OLE 2.0 are entirely Unicode, and since Version 4, Visual Basic has represented strings in Unicode internally.

Because of the way VB handles strings internally, the operation of certain functions (such as Chr) has changed when compared to VB's early versions. For example, assigning the return value of Chr to a string data type results in a string one byte in length under Windows 95, a non-Unicode system; this is the traditional behavior of Chr. But under Windows NT, a Unicode system, it results in a string two bytes in length. To cope with the extra demands of Unicode, VB4 introduced a number of new functions, including AscB, AscW, ChrB, and ChrW. The "W"-suffixed functions handle the two bytes of Unicode characters. The "B"-suffixed functions work with Byte data, but, like the Asc and Chr functions, handle only the first byte of the byte string passed to them.


See Also

Chr, ChrB, ChrW Functions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.19.17