Re:UTF-8 – "The most elegant hack"
by Patrick_Devine on Saturday September 28, 2013 @08:22PM
(#6463607)
I love that UTF-8 kind of followed in the tradition already set by the guys who came up with ASCII. ASCII itself is brilliant in that they aligned characters to be easy to test. Take upper and lower case 'A' which are 0x41 and 0x61 respectively:
0x41 0100 0001
0x61 0110 0001
So if you want to test whether a character is uppercase or not, you can just & 0x20.
Another one is how the number digits got lined up, which start at 0x30 for 0 to 0x39 for 9, which look like:
0x30 0011 0000
0x31 0011 0001
0x32 0011 0010
etc.
You can just read off the first nibble to convert from ASCII to an int which is pretty cool.
0x41 0100 0001 0x61 0110 0001
So if you want to test whether a character is uppercase or not, you can just & 0x20.
Another one is how the number digits got lined up, which start at 0x30 for 0 to 0x39 for 9, which look like:
0x30 0011 0000 0x31 0011 0001 0x32 0011 0010 etc.
You can just read off the first nibble to convert from ASCII to an int which is pretty cool.