Refracted Paladin
Refracted Paladin

Reputation: 12216

(Char)174 returning the value of (Char)0174, why?

I am working with string delimiters and one of them is « or 174. However, when I step through my code it looks like this in the debugger, ®, which is 0174. See here for Codes.

This is how I'm doing it in code for reference:

string fvDelimiter = ((char)174).ToString();

Upvotes: 4

Views: 1681

Answers (3)

Konrad Kokosa
Konrad Kokosa

Reputation: 16878

It is all about character encoding. 174 (AE in hex) is ® in Unicode, which is used internally in string by default. But it is « in Extended ASCII code.

Please refer this difference in article you've provided:

Inserting ASCII characters

To insert an ASCII character, press and hold down ALT while typing the character code. For example, to insert the degree (º) symbol, press and hold down ALT while typing 0176 on the numeric keypad.

Inserting Unicode characters

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.

Upvotes: 6

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

For the simple reason that char in c# represents a unicode character code which match perfectly 0174 and not an ASCII code which is 174

here a code to let you understand more what I mean

            var chrs = System.Text.Encoding.ASCII.GetChars( new byte[]{0174});
            var chrsUtf = System.Text.Encoding.UTF8.GetChars(new byte[] { 0174 });
            var chrsUnicode = System.Text.Encoding.Unicode.GetChars(new byte[] { 0174 }); 
            Debug.WriteLine(chrs[0].ToString());
            Debug.WriteLine(chrsUtf[0].ToString());
            Debug.WriteLine(chrsUnicode[0].ToString());

Upvotes: 1

Norman Skinner
Norman Skinner

Reputation: 6985

From what I have seen it is based on the font being used. On Windows I use "Character Map" to check them out. For example the "«" character in the font Calibri is 171.

Upvotes: 1

Related Questions