• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking Printable Characters

 
Tim Eapen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am a student and a Java newbie. What I am trying to do is write a program that does a hex dump of an input file and also print out the contents of the file in regular ASCII text. It mostly works except the output file contains some garbage characters so I figure I have to check each individual character and only print it if it is in 'printable form'. How would I do this? I'm not shure how I would go about checking each individual byte in the stream and see if it is a valid ASCII byte. I'm just using a PrintStream object to print the output file.

thx,
Tim
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All bytes between 0 and 127 are valid ASCII bytes, in that ASCII defines a meaning for those bytes. The ones between 128 and 255 aren't. But your problem seems to be about displaying those bytes. Whether they are printable or not rather depends on where you are trying to print them and what font is being used. However perhaps reading the Wikipedia article on ASCII might improve your understanding:

http://en.wikipedia.org/wiki/ASCII
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way my text editor can display the first 8 columns of a file:

You can work out the one == 6F6E65, space == 20, etc. When it comes to something unprintable like the tab or bell characters 09 and 07, it shows a question mark in the display side but still has the correct hex ascii value.

Is this kind of what you had in mind?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Whether they are printable or not rather depends on where you are trying to print them and what font is being used.


For instance, in my Linux console I can see unicode characters, like those of the greek alphabet, but I can�t do that on Windows console.

Long Live Linux!
 
Tim Eapen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy - I found the solution in the Character class. Here it comes....



Thanks for all your help!
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. I was assuming you would consider "*" and "#" as printable characters, probably that's why I didn't come up with that solution.
 
Tim Eapen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

You are right....here is something better:

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the !ISO would be enough test. Anything that is character or digit also passes that test. Be sure to make yourself a test input of all possible values 0..255 and see what prints.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, after looking at the descriptions of ASCII characters, I would do this:This is hard-coding but ASCII isn't going to change so I don't think it's a bad thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!