File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes getting ascii char from int only seems to work if int is less than 127 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "getting ascii char from int only seems to work if int is less than 127" Watch "getting ascii char from int only seems to work if int is less than 127" New topic
Author

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

Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: getting ascii char from int only seems to work if int is less than 127
 
Similar Threads
ASCII characters AND Java characters
ArrayList Problem
Passing Parameters from Java to JavaScript
Question re casting from Random.nextInt to char
s:if test troubles, How to test these expression