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.
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.