File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Ascii values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ascii values" Watch "Ascii values" New topic

Ascii values

Regina Olguin

Joined: Oct 03, 2003
Posts: 5
I'm trying to take an encryption function from a Powerbuilder app to Java. I can't use the Security classes because it complicates things for other apps still in PB. I also don't have time to figure it out as I'm a bit new to Java, though not to programming.

So, is there a way I can get an ascii value of a char? I know Java likes unicode. Maybe I could first get the unicode value and then get the ascii value for that?

Many thanks.
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
You might be interested in the java.nio.charset package, which allows you to do things like specify encoding (ascii, etc) for streams. Take a look at this tutorial:

which specifically talks about porting legacy apps to java using the java.nio.charset package.
[ May 19, 2004: Message edited by: Jason Fox ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
All ASCII char values are identical to the corresponding Unicode values, for 0-127. So there's really no conversion to do - other than maybe cast to byte:

If you encounter characters in the range 128-255, those aren't really ASCII - but they can be represented in several other ASCII-based encodings, like ISO-8859-1 or Cp-1052 (Windows extended ASCII). However you won't be able to do this correctly unless you determine exactly which encoding is really being used.

Note also that the NIO Charset class is not really necessary; you can also perform conversion with classes like InputStreamReader and OutputStreamWriter. And the String class has useful methods, like new String(byte[], String) and getBytes(String). The NIO classes give you greater control over how the conversion takes place (e.g. what do do if there's an error) and it can be more efficient (especially if you're using NIO classes for other parts of your program). But if you're using an older JDK (before 1.4) or if you're new to NIO, the other classes I mentioned should work fine.
[ May 19, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
True, the NIO package is not necessary, but I thought the tutorial would be useful because it talks specifically about porting legacy code into Java.
I agree. Here's the link:
subject: Ascii values
It's not a secret anymore!