Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting ((char) a) where a is any number between int range

I know that ASCII codes are between 0-127 in decimal and 0000 0000 to 0111 1111 in binary, and that values between 128-255 are extended ASCII.

I also know that int accepts 9 digits(which I was wrong the range int is between(-2,147,483,648 to 2,147,483,647)), so if we cast every number between (0-MaxintRange) to a char, there will be many many symbols; for example:

(char)999,999,999 gives 짿 which is a Korean symbol (I don't know what it even means; Google Translate can't find any meaning!).

The same thing happens with values between minintrange to 0.

It doesn't make sense that those symbols were input one by one.

I don't understand - how could they assign those big numbers to have its own character?

like image 825
Azad Avatar asked Dec 01 '22 03:12

Azad


1 Answers

I don't understand how they assign those big numbers to have it's own symbol?

The assignments are made by the Unicode consortium. See http://unicode.org for details.

In your particular case however you are doing something completely nonsensical. You have the integer 999999999 which in hex is 0x3B9AC9FF. You then cast that to char, which discards the top four bytes and gives you 0xC9FF. If you then look that up at Unicode.org: http://www.unicode.org/cgi-bin/Code2Chart.pl and discover that yes, it is a Korean character.

Unicode code points can in fact be quite large; there are over a million of them. But you can't get to them just by casting. To get to Unicode code points that are outside of the "normal" range using UTF-16 (as C# does), you need to use two characters. See http://en.wikipedia.org/wiki/UTF-16, the section on surrogate pairs.

To address some of the other concerns in your question:

I know that ACCII codes are between (0-127) in decimal and (0000 0000 to 0000 1111) in binary.

That's ASCII, not ACCII, and 127 in binary is 01111111, not 00001111

Also we know that int accepts 9 digits, so if we cast every number between

The range of an int is larger than that.

don't know what it mean even Google translate can't find any meaning

Korean is not like Chinese, where each glyph represents a word. Those are letters. They don't have a meaning unless they happen to accidentally form a word. You'd have about as much luck googling randomly chosen English letters and trying to find their meaning; maybe sometimes you'd choose CAT at random, but most of the time you'd choose XRO or some such thing that is not a word.

Read this if you want to understand how the Korean alphabet works: http://en.wikipedia.org/wiki/Hangul

like image 88
Eric Lippert Avatar answered Dec 04 '22 23:12

Eric Lippert