Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ascii values

 
Regina Olguin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regina
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

http://www-106.ibm.com/developerworks/java/library/j-mer1022.html

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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic