wood burning stoves 2.0*
The moose likes Java in General and the fly likes Probably Simple But I Need Assistance - Printing Out All Escape Characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Probably Simple But I Need Assistance - Printing Out All Escape Characters" Watch "Probably Simple But I Need Assistance - Printing Out All Escape Characters" New topic
Author

Probably Simple But I Need Assistance - Printing Out All Escape Characters

John Douglas
Greenhorn

Joined: Mar 18, 2010
Posts: 7
Hello All

I am attempting to write a very simple Java program that prints out all escape characters between 0 and 255. Here is what I have but it, obviously, does not work:


Whenever I attempt to compile, I get this error:



Like I said, this is probably easy but I'm not sure how to do it.

Thanks for reading and have a good day.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
The \ sequence is interpreted by the compiler before compilation starts, so it reads \u00 as an error because \u is supposed to be followed by 4 (hexadecimal) digits/0. I am not convinced I would call those escape characters; that range includes mostly ordinary characters.

You realise you can cast an int to a char and print that with the %c tag? That might be a lot easier.
John Douglas
Greenhorn

Joined: Mar 18, 2010
Posts: 7
Converting an int to a char worked perfectly ... thank you. I was simply under the assumption that I had to use the escape character and the hexidecimal value of the character I was wanting to print.

Thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
You're welcome Have a look at the Java™ Language Specification definition of what a char really is.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Your code will have a problem if the HEX representation is just one character. Fortunately, Java 6 came with java.util.Formatter which is integrated into String.format and PrintStream.printf:
This will print \u, followed by i in 4 HEX digits (x makes it HEX, 04 gives it width 4 filling empty slots with 0).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Yes, I tried that earlier. If you print "\\" + 'u' + String.format("%04x", i), you get \u0056 or similar displayed on screen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probably Simple But I Need Assistance - Printing Out All Escape Characters
 
Similar Threads
need help. please. PLEASE!
How much Unicode knowledge do I need for the exam?
the artist formerly known as Prince
String.split("").......Confusion.
how to perform split operations for special characters ~!^* etc..?