How to Get ascii code of special character in C#

I want to get the ascii code of the special characters of this example "º" and the result should be 186 but my code gives me 63 please help me.

Here is my Code:

string prText ="º";
var tempVal = new byte[1];
byte[] Asc = Encoding.ASCII.GetBytes(prText);
foreach (byte z in Asc)
{
   tempVal[0] = z;
}

Upvotes: 0

Views: 2174

Answers (2)

I solved my problem using this code to get the correct value

string prText ="º";
int int2 = Char.ConvertToUtf32(prText, 0)

and result will be 186

Upvotes: 0

drf
drf

Reputation: 8699

The degree symbol is not representable as an ASCII character. From the documentation

ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is translated to an ASCII question mark ("?").

You may want to use ANSI encoding with the Windows-1252 code page. In this encoding, the degree symbol is represented as 0xBA (186).

Upvotes: 3

Related Questions