Home
Map
Val, Asc and AscW FunctionsUse the Val, Asc and AscW built-in Functions to convert characters to other types.
VB.NET
This page was last reviewed on Mar 28, 2022.
Val, Asc. In VB.NET Val, Asc and AscW convert characters. From the Char, we get either its integer representation or its numeric representation.
Conversion functions. Using these Functions, we convert characters into the appropriate integer value. We can go back and forth with these functions.
Chr
Example. We use these functions with two characters. The character "a" has a Val of 0 and an Asc of 97. In ASCII, the letter "a" is represented with the number 97.
Info The Val function returned zero because "a" is not a number itself. Val just returns 0 on non-digits.
And The second Char in this example contains the digit "2". Val here returns 2. This is the digit contained in the character.
Finally For "2" the AscW function returns 50. This is the ASCII numeric representation for the character "2".
Module Module1 Sub Main() ' Use Val and AscW on char. Dim c As Char = "a"c Dim i As Integer = Val(c) Dim a As Integer = Asc(c) Console.WriteLine(c) Console.WriteLine(i) Console.WriteLine(a) ' Another character. Dim c2 As Char = "2"c Dim i2 As Integer = Val(c2) Dim a2 As Integer = AscW(c2) ' AscW is similar to Asc Console.WriteLine(c2) Console.WriteLine(i2) Console.WriteLine(a2) End Sub End Module
a 0 97 2 2 50
A review. The Val, Asc and AscW Functions are useful in different situations. If you want to convert a Char to its underlying integer representation, the Asc and AscW functions are ideal.
Final note. If you want to get the number from the Char and turn it into an Integer, the Val function is best. The Val, Asc and AscW functions are only available in VB.NET, not C#.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 28, 2022 (edit link).
Home
Changes
© 2007-2024 Sam Allen.