Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I print extended ascii characters in Java

 
Samuel Thomas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I would like to print extended ascii characters(characters between 127 and 255). Unfortunately I am not able to get the corresponding ascii character symbols for the following values 129,141,143,144,157. I am getting the correct values for all the other numbers,
except for these numbers where a ? is returned.
for( int i=127;i < 256;i++)
{
byte[] data = new byte[1];
data[0]= (byte)i;
System.out.println("The character " + new String(data));
}
Could any one tell me where I went wrong ?
Please do help me with an idea of getting the values for the missed numbers?
Thanking you in advance
Regards
Samuel
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's more than one "extended ASCII" out there. Probably the default encoding on your machine is different from what you are expecting. Instead use the String constructor that allows you to specify an encoding:
<code><pre> System.out.println(new String(data, "US-ASCII"));
System.out.println(new String(data, "ISO-LATIN-1"));
</pre></code>
For comparison, you can find out the name of the default encoding on your system with
<code><pre> System.out.println("Default: " + new InputStreamReader(System.in).getEncoding());</pre></code>
More info on encoding can be found here and here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic