2

I'm currently writing a program to print the values of the ascii table, and have run in to the issue that characters 0-32 will just leave a blank space in my chart, or at the worst, it will print the backspace, new line, tab, or similar characters and disorganize my chart.

Here's the line that prints each segment of my table inside of a loop, Where i is the decimal representation of the character I want to print.

System.out.print(i+"\t"+((char) i)+"\t");

Is there any easier way to print each the control characters without reformatting my table other than a switch structure that will handle all characters between 0-32?

ryzn
  • 21
  • 2
  • You could also just print their [control picture](http://unicode.org/charts/PDF/U2400.pdf) characters, instead. (Of course, printing anything to a console requires that the program's output stream encoding matches the console's encoding (locale) and the console's font supports the characters you are printing.) – Tom Blodget Apr 01 '16 at 00:14

3 Answers3

4

Have you tried StringEscapeUtils.escapeJava in Java lib "org.apache.commons.lang"?

System.out.print(StringEscapeUtils.escapeJava(string_containing_ctrl_chars));
Suparna
  • 1,132
  • 1
  • 8
  • 20
1

Apache commons has StringEscapeUtils.escapeJava(String)

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

You could have an if statement

if(i >= 0 && i <= 32) {
  System.out.print(i+"\t Na \t"); //or change Na to whatever you want
} else {
  System.out.print(i+"\t"+((char) i)+"\t");
}
Blubber
  • 1,375
  • 1
  • 15
  • 32