Convert String from ASCII to EBCDIC in Java?
Solution 1
JTOpen, IBM's open source version of their Java toolbox has a collection of classes to access AS/400 objects, including a FileReader and FileWriter to access native AS400 text files. That may be easier to use then writing your own conversion classes.
From the JTOpen homepage:
Here are just a few of the many i5/OS and OS/400 resources you can access using JTOpen:
- Database -- JDBC (SQL) and record-level access (DDM)
- Integrated File System
- Program calls
- Commands
- Data queues
- Data areas
- Print/spool resources
- Product and PTF information
- Jobs and job logs
- Messages, message queues, message files
- Users and groups
- User spaces
- System values
- System status
Solution 2
Please note that a String in Java holds text in Java's native encoding. When holding an ASCII or EBCDIC "string" in memory, prior to encoding as a String, you'll have it in a byte[].
ASCII -> Java: new String(bytes, "ASCII") EBCDIC -> Java: new String(bytes, "Cp1047") Java -> ASCII: string.getBytes("ASCII") Java -> EBCDIC: string.getBytes("Cp1047")
Solution 3
You should use either the Java character set Cp1047 (Java 5) or Cp500 (JDK 1.3+).
Use the String constructor: String(byte[] bytes, [int offset, int length,] String enc)
Solution 4
package javaapplication1;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class ConvertBetweenCharacterSetEncodingsWithCharBuffer {
public static void main(String[] args) {
//String cadena = "@@@@@@@@@@@@@@@ñâæÃÈÄóöó@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ÔÁâãÅÙÃÁÙÄ@ÄÅÂÉã@âæÉãÃÈ@@@@@@@@";
String cadena = "ñâæÃÈÄóöó";
System.out.println(Convert(cadena,"CP1047","ISO-8859-1"));
cadena = "1SWCHD363";
System.out.println(Convert(cadena,"ISO-8859-1","CP1047"));
}
public static String Convert (String strToConvert,String in, String out){
try {
Charset charset_in = Charset.forName(out);
Charset charset_out = Charset.forName(in);
CharsetDecoder decoder = charset_out.newDecoder();
CharsetEncoder encoder = charset_in.newEncoder();
CharBuffer uCharBuffer = CharBuffer.wrap(strToConvert);
ByteBuffer bbuf = encoder.encode(uCharBuffer);
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
//System.out.println("Original String is: " + s);
return s;
} catch (CharacterCodingException e) {
//System.out.println("Character Coding Error: " + e.getMessage());
return "";
}
}
}
Solution 5
You can create one yoursef with this translation table.
But here is a site that has a link to a Java example.
Petr Prazak
I'm a programmer, speaker, runner, movie fan, Belgium beer connoisseur, cyclist and all round techie geek. I love all things mobile, especially Android and mobile security. Co-authored The Android Security CookBook and founded/run SWMobile
Updated on August 25, 2020Comments
-
Petr Prazak almost 4 years
I need to write a 'simple' util to convert from ASCII to EBCDIC?
The Ascii is coming from Java, Web and going to an AS400. I've had a google around, can't seem to find a easy solution (maybe coz there isn't one :( ). I was hoping for an opensource util or paid for util that has already been written.
Like this maybe?
Converter.convertToAscii(String textFromAS400) Converter.convertToEBCDIC(String textFromJava)
Thanks,
Scott
-
Petr Prazak over 15 yearsWe are using the JTopen tool box and it is doing some of the convertion/mapping, it's just it seems to incorrectly map £,$,[ and ^
-
matbrgz over 15 yearsSounds like your AS/400 is incorrectly configured regarding its native tongue. If it is set up correctly jt400.jar will not require any other tweaking.
-
matbrgz over 15 yearsThere are many EBCDIC code tables. It is very tedious to get right manually.
-
Mike Wills over 14 yearsYes, the conversion should happen basically automatically. If it isn't, something isn't setup right.
-
Bill the Lizard over 12 yearsThe second link is dead. Do you know where it went? Can you post the example here?
-
zaid hussian over 12 yearsThe Java character sets that start with "CP" refer to IBM CCSIDs. Some documentation of these can be found at www-03.ibm.com/systems/i/software/globalization/ccsid_list.html and www-03.ibm.com/systems/i/software/globalization/codepages.html CP1047 appears to refer to 01047, "Latin 1/Open Systems".
-
Markus W Mahlberg about 9 yearsWelcome to SO! Explaining your solution is not required, but considers good practice, with the nice side effects that people learn to understand and hence upvote your answer. ;)
-
some_coder over 7 years@AlanKrueger as of today these links are dead. Thats really too bad.
-
VJ. about 7 years@some_coder - new link is www-01.ibm.com/software/globalization/cp/cp_cpgid.html
-
Dominique Fortin almost 7 yearsYou forgot Cp037 (we have that one). You should suggest that the person verifies what characterset is being used.
-
Pryftan almost 3 years@BilltheLizard web.archive.org/web/20080112153232/https://reply42.com/…. Maybe I should edit the answer but...
-
Bill the Lizard almost 3 yearsWeb Archive links are fine if the original is no longer online. I'd go ahead and edit the answer. Also, you could have waited until Nov 8 to reply back on the 10th anniversary of my comment. ;p