Home
Map
ASCII TableGenerate an ASCII table of the 128 ASCII codes. Perform manipulations of ASCII chars.
Java
This page was last reviewed on Sep 25, 2022.
ASCII table. In computer programs, we often manipulate characters that are stored in ASCII. The letter "a" is the value 97. And "b" is 98.
Notes on table. Some values in ASCII are represented with the string "control" or "whitespace." These are not easy to display in a web page file.
The table. There are 128 values in ASCII. This is a small subset of Unicode. 2 bytes are needed for Unicode, but ASCII requires just one byte.
Decimal ASCII Hex 0 control 0 1 control 1 2 control 2 3 control 3 4 control 4 5 control 5 6 control 6 7 control 7 8 control 8 9 \t 9 10 \n a 11 whitespace b 12 \f c 13 \r d 14 control e 15 control f 16 control 10 17 control 11 18 control 12 19 control 13 20 control 14 21 control 15 22 control 16 23 control 17 24 control 18 25 control 19 26 control 1a 27 control 1b 28 whitespace 1c 29 whitespace 1d 30 whitespace 1e 31 whitespace 1f 32 space 20 33 ! 21 34 " 22 35 # 23 36 $ 24 37 % 25 38 & 26 39 ' 27 40 ( 28 41 ) 29 42 * 2a 43 + 2b 44 , 2c 45 - 2d 46 . 2e 47 / 2f 48 0 30 49 1 31 50 2 32 51 3 33 52 4 34 53 5 35 54 6 36 55 7 37 56 8 38 57 9 39 58 : 3a 59 ; 3b 60 < 3c 61 = 3d 62 > 3e 63 ? 3f 64 @ 40 65 A 41 66 B 42 67 C 43 68 D 44 69 E 45 70 F 46 71 G 47 72 H 48 73 I 49 74 J 4a 75 K 4b 76 L 4c 77 M 4d 78 N 4e 79 O 4f 80 P 50 81 Q 51 82 R 52 83 S 53 84 T 54 85 U 55 86 V 56 87 W 57 88 X 58 89 Y 59 90 Z 5a 91 [ 5b 92 \ 5c 93 ] 5d 94 ^ 5e 95 _ 5f 96 ` 60 97 a 61 98 b 62 99 c 63 100 d 64 101 e 65 102 f 66 103 g 67 104 h 68 105 i 69 106 j 6a 107 k 6b 108 l 6c 109 m 6d 110 n 6e 111 o 6f 112 p 70 113 q 71 114 r 72 115 s 73 116 t 74 117 u 75 118 v 76 119 w 77 120 x 78 121 y 79 122 z 7a 123 { 7b 124 | 7c 125 } 7d 126 ~ 7e 127 control 7f
Example program. Next we introduce the Java program that generated the above table. The program displays the header for the table, then loops through 128 values to display the rows.
Detail We invoke the Character.isWhitespace method to determine if a char is whitespace.
Detail We use a switch to help display a whitespace char. Some sequences (like a vertical tab) are just displayed as "whitespace."
switch
Detail This built-in method detects some characters in ASCII that are also not letters, digits or punctuation.
Finally We display everything into rows. We use left-padding in our format string to make things look nice.
String.format
public class Program { public static void main(String[] args) { // Write the header. System.out.printf("%1$-8s %2$-10s %3$s\n", "Decimal", "ASCII", "Hex"); // Loop over all possible ASCII codes. int min = 0; int max = 128; for (int i = min; i < max; i++) { char c = (char) i; String display = ""; // Figure out how to display whitespace. if (Character.isWhitespace(c)) { switch (c) { case '\t': display = "\\t"; break; case ' ': display = "space"; break; case '\n': display = "\\n"; break; case '\r': display = "\\r"; break; case '\f': display = "\\f"; break; default: display = "whitespace"; break; } } else if (Character.isISOControl(c)) { // Handle control chars. display = "control"; } else { // Handle other chars. display = Character.toString(c); } // Write a string with padding. System.out.printf("%1$-8d %2$-10s %3$s\n", i, display, Integer.toHexString(i)); } } }
See above table.
ASCII manipulation. We can discover optimizations by inspecting the ASCII table. For example, we can convert the letters "a" through "z" to 0-26 by subtracting 97.
Digit conversion. To convert from digit chars "0" through "9" to actual digits, we must subtract 48. This is a simple conversion.
A review. An ASCII table helps in many aspects of computer programming. We can develop optimizations with ASCII codes to improve program performance.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 25, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.