VB.NET Chr Function Example

The VB.NET programming language

Chr deals with Integers and Chars. You want to convert your Integer into a Char in your VB.NET program. By using the Chr function, you can take an Integer that represents a specific character and then acquire the actual Char value.

This VB example program demonstrates the Chr built-in Function.

Example

First, this program declares three local variables. The first variable c is assigned to the character "x"; the second variable i is set to the ASCII value underlying the "x"; and the third variable h shows how to convert an integer back into a character.

Program that uses Chr function [VB.NET]

Module Module1
    Sub Main()
	Dim c As Char = "x"c ' Input character.
	Dim i As Integer = Asc(c) ' Convert to ascii integer.
	Dim h As Char = Chr(i) ' Convert ascii integer to char.

	Console.WriteLine(c)
	Console.WriteLine(i)
	Console.WriteLine(h)
    End Sub
End Module

Output

x
120
x
Programming tip

The letter "x". You can see that the ASCII representation for "x" is equal to 120. Then, the character representation (Chr) of 120 is equal to "x". So Asc and Chr can be used to reverse each other's effects on a value.

Summary

Here, we showed how you can use Asc and Chr to manipulate ASCII integers for characters in the VB.NET language. In some algorithms, the Integer representation of a Char is more useful; you can use Asc and Chr in these cases to simplify your program.

Val and Asc Function Examples VB.NET Tutorials
.NET