• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting ascii char from int only seems to work if int is less than 127  RSS feed

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get the ASCII value based on an int and it seems correct until int is greater than 126. For example with this code:

I get: testchar = the character at dec position 138 on the ascii table (I can't type it here)


With this code:


I get the correct character: ~

Can I get advice on what I may be doing wrong? Thanks.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Walin:
... Can I get advice on what I may be doing wrong? Thanks.

It's not you. It's the application that's trying to display the char. Your command line app (or whatever you're using) just doesn't know how to represent char values above a certain point.

If you put this in a Swing program, it should display fine. For example...

[ February 12, 2008: Message edited by: marc weber ]
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem is that "ASCII" is meaningless beyond decimal 127. (And 127 is the DELETE character, so you can't see it.) There have been a number of different versions of "extended ASCII" assigning characters to values 128-255, but there is no one standard. Java uses Unicode, which for the first 256 characters is the same as ISO-8859-1.

The fact that you refer to a character at position 138 on the ASCII table suggests you're not looking at a Unicode table though, since that is an unprintable character in Unicode. A commonly-encountered impostor is Cp-1252, the Windows nonstandard version of Latin-1. Does that table match what your "ASCII table" says?

To convert Cp-1252 values to Unicode, there are several ways. One is:

If your "ASCII table" uses some other encoding besides Cp-1252, you will need to discover what encoding it is before you can convert properly. It may be helpful to tell us what operating system you're using, and what country you're in.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some more information to fill in the blanks. The table I was looking at is this:
web page
I'm actually displaying what I get with System.out.println() for now. Later I'll be writing it to a file. The operating system this is running on is Windows XP Professional. I'm going to read through your advice in more detail to see if it helps, but wanted to answer your questions first in case that will affect your advice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!