Visual Basic Language Reference  

Chr, ChrW Functions

Returns the character associated with the specified character code.

Public Function Chr(ByVal CharCode As Integer) As Char
Public Function ChrW(ByVal CharCode As Integer) As Char

Parameter

CharCode
Required. An Integer expression representing the code point, or character code, for the character. If CharCode is outside the range -32768 through 65535, an ArgumentException error occurs.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 CharCode < -32768 or > 65535.

Remarks

The asymmetric range accepted for CharCode compensates for the storage differences between the Short and Integer data types. For example, &H8E01 is -29183 as a Short but +36353 as an Integer. This also facilitates compatibility with Visual Basic 6.0.

Chr uses the Encoding class in the System.Text namespace to determine if the current thread is using a single-byte character set (SBCS) or a double-byte character set (DBCS). It then takes CharCode as a code point in the appropriate set. The range can be 0 through 255 for SBCS characters and -32768 through 65535 for DBCS characters. The returned character 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.

ChrW takes CharCode as a Unicode code point. The range is independent of the culture and code page settings for the current thread. Values from -32768 through -1 are treated the same as values in the range +32768 through +65535.

Numbers from 0 through 31 are the same as standard, nonprintable ASCII codes. For example, Chr(10) returns a linefeed character.

Example

This example uses the Chr function to return the character associated with the specified character code.

Dim MyChar As Char
MyChar = Chr(65)   ' Returns "A".
MyChar = Chr(97)   ' Returns "a".
MyChar = Chr(62)   ' Returns ">".
MyChar = Chr(37)   ' Returns "%".

See Also

Asc, AscW Functions | Str Function | Conversion Functions | Type Conversion Functions | System.Text Namespace | Encoding Class | System.Globalization Namespace | CultureInfo Class | TextInfo.ANSICodePage Property | ArgumentException