how to get the ASCII code of a character

  • Thread starter Thread starter Anibal David Acosta F.
  • Start date Start date
A

Anibal David Acosta F.

I use C# and need to get the ASCII code of a 8bit character.

When I use the Convert.ToByte() function, it throw an error when the
character is above the 127, for example the char: Ñ

The function: System.Text.ASCIIEncoding.Default.GetBytes() work fine but it
return an array of bytes and I need to use in this way:
System.Text.ASCIIEncoding.Default.GetBytes("Ñ")(0)

And i think that is very poor for performance.

Thanks guys.

AA
 
Anibal David Acosta F. said:
It work fine, but the problem is that the ToChar() function convert to a
Unicode character and when I try to convert to a Byte an error is generated
because the char code is too big, in my case (unicode 8224) for the
character ASCII 134.

There *is* no ASCII 134.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

By the way, the easier way to get a char from a string is just:

char x = myString[20];
 
Rob Epstein said:
Then you can call:

int i = (int) x; // returns codepage code for given character

No, it returns the *Unicode* value for the given character.
 
Back
Top