function Chr()

  • Thread starter Thread starter Elephant
  • Start date Start date
E

Elephant

Hello,

The function Microsoft.VisualBasic.Chr( ), is there a similar function in
System.Text ? How does this work?

Thank you,

me.
 
If you put a hard-coded value inside Chr() such as

Chr(65)

then the compiler will remove the Chr() call and replace it with a
constant:

"A"c

If the argument is a variable, then it compiles it to a call to
Strings.Chr() inside Microsoft.VisualBasic.

For character codes 0-127, then Chr() is the same as Convert.ToChar().

Dim i as Integer = 65
Dim c as Char = Chr(i)

is the same as

Dim i as Integer = 65
Dim c as Char = Convert.ToChar(i)

For higher characters, Chr() does some complicated stuff with unicode
character resolution and the current code page.

HTH,

Sam
 
Samuel R. Neff said:
For higher characters, Chr() does some complicated stuff with unicode
character resolution and the current code page.

In addition to that, VB provides a 'ChrW' function too.
 
I was wondering about that.. what's the differene between passing a
127 unicode character to Chr() and ChrW()? Chr() does lots of extra
processing and ChrW() just passes it along to Convert.ToChar().
What's this extra processing do and when would one use Chr() over
ChrW()?

Thanks,

Sam
 
Elephant,
In addition to the other comments.

As Samuel stated: Chr does Ansi encoding using the current code page, while
ChrW does Unicode encoding.

ChrW & Convert.ToChar do not need to use an Encoding object per se, as the
integer code point for a character is simply the character. Chr would need
an encoding object as Samuel stated characters 128 to 255 use the Ansi
Encoding object for the current code page. I understand that both Chr & ChrW
handle negative numbers in a consistent manner, where as Convert.ToChar will
throw an exception.


Encoding within .NET is handled by the System.Text.Encoding class.

Encoding.Default offers an Encoding object that represents the current code
page, while Encoding.Unicode offers an Encoding object that represents the
Unicode character set.

Normally you use an Encoding object to convert an array of Bytes to & from a
String. By string I mean both a String or an array of Chars.

Dim bytes() As Byte
Dim name As String = "Jay B. Harlow"
Dim ansi As Encoding = Encoding.Default
bytes = ansi.GetBytes(name)
name = ansi.GetString(bytes)

For individual characters using Chr or ChrW is easier.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top