10

This may be an immature question, may be am missing something but my question is

Trying convert a char to int to get the ASCII value of that char, in most cases I get correct/expected ASCII code for particular char, in some cases I don't. Can someone explain me why?

Examples:

// Example 1:-   

Console.WriteLine((int)'a');

// gives me 97 perfect!

// Example 2:-

Console.WriteLine((char)1); gives me ☺

// now 

Console.WriteLine((int )'☺');

// this should give me 1, instead it gives me 9786 why?

this happens to ASCII > 127 or ASCII < 32.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
warrior
  • 606
  • 2
  • 8
  • 23

3 Answers3

12

\01 is unprintable character, as result console is free to use any substitution to make it visible.

Also default Windows console is not very Unicode friendly, so strange things happen when you print characters outside of default printable ASCII range.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
4

Firstly, there's no such thing as "ASCII > 127" - ASCII only goes up to 127 (or 126; I can never remember whether delete is properly part of ASCII).

Basically when you print out non-printable characters such as U+0001 ("Start of heading") it's up to the display device to determine what to do with that. Some consoles will print squares, some will print smileys, etc. You certainly shouldn't expect it to be reversible. You can expect the conversion itself to be reversible in code though:

char c = '\u0001';
int i = c; // i = 1
char d = (char) i;
Console.WriteLine(c == d); // True
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @DineshB: That's not ASCII. "Extended ASCII" is a *ghastly* term which generally means "any single-byte encoding which is compatible with ASCII up to 127." See http://en.wikipedia.org/wiki/ASCII - "ASCII includes definitions for 128 characters" – Jon Skeet Mar 07 '13 at 23:24
  • Gotchu, my bad then to quote it as ASCII – warrior Mar 07 '13 at 23:32
1

In answer to your question, why does Console.WriteLine((int )'☺'); return 9786, it is because '☺' in Unicode is represented by bytes 58 38, which as represented by an integer is 9786.

Chars in c# are Unicode characters.

bobbymond
  • 81
  • 3