Display non printable characters
Sometimes we need to display non printable characters (ASCII code < 32 or ASCII code > 127) for debug reason. Of course we can escape the characters to hexical code, such as '\x0a\x0d'
, but sometimes using “Control Pictures” code page in Unicode might be a better idea, because it requires less screen space.
ASCII | Picture | Hex | HTML Entity |
---|---|---|---|
0 | ␀ | 2400H | ␀ |
1 | ␁ | 2401H | ␁ |
2 | ␂ | 2402H | ␂ |
3 | ␃ | 2403H | ␃ |
4 | ␄ | 2404H | ␄ |
5 | ␅ | 2405H | ␅ |
6 | ␆ | 2406H | ␆ |
7 | ␇ | 2407H | ␇ |
8 | ␈ | 2408H | ␈ |
9 | ␉ | 2409H | ␉ |
10 | ␊ | 240AH | ␊ |
11 | ␋ | 240BH | ␋ |
12 | ␌ | 240CH | ␌ |
13 | ␍ | 240DH | ␍ |
14 | ␎ | 240EH | ␎ |
15 | ␏ | 240FH | ␏ |
16 | ␐ | 2410H | ␐ |
17 | ␑ | 2411H | ␑ |
18 | ␒ | 2412H | ␒ |
19 | ␓ | 2413H | ␓ |
20 | ␔ | 2414H | ␔ |
21 | ␕ | 2415H | ␕ |
22 | ␖ | 2416H | ␖ |
23 | ␗ | 2417H | ␗ |
24 | ␘ | 2418H | ␘ |
25 | ␙ | 2419H | ␙ |
26 | ␚ | 241AH | ␚ |
27 | ␛ | 241BH | ␛ |
28 | ␜ | 241CH | ␜ |
29 | ␝ | 241DH | ␝ |
30 | ␞ | 241EH | ␞ |
31 | ␟ | 241FH | ␟ |
32 | ␠ | 2420H | ␠ |
127 | ␡ | 2421H | ␡ |
32 | ␣ | 2423H | ␣ |
And here is a simple method to convert the characters in a string.
@implementation ControlPictures
+ (unichar)controlPictureByCharacter:(unichar)ch {
if (ch < 33) {
return (unichar)(ch + 0x2400);
} else if (ch == 127) {
return (unichar)0x2421;
} else {
return ch;
}
}
+ (NSString *)stringByReplacingControlCharacters:(NSString *)str {
NSMutableString *result = [@"" mutableCopy];
for (NSInteger idx = 0; idx < str.length; idx++) {
[result appendFormat:@"%C", [ControlPictures controlPictureByCharacter:[str characterAtIndex:idx]]];
}
return result;
}
@end