Sunday, March 6, 2011

Convert String from ASCII to EBCDIC in Java?

Hello,

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

From stackoverflow
  • It should be fairly simple to write a map for the EBCDIC character set, and one for the ASCII character set, and in each return the character representation of the other. Then just loop over the string to translate, and look up each character in the map and append it to an output string.

    I don't know if there are any converter's publicly available, but it shouldn't take more than an hour or so to write one.

  • You can create one yoursef with this translation table.

    But here is a site that has a link to a Java example.

  • 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)

  • 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
    scottyab : We are using the JTopen tool box and it is doing some of the convertion/mapping, it's just it seems to incorrectly map £,$,[ and ^
    Thorbjørn Ravn Andersen : Sounds 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.
    Mike Wills : Yes, the conversion should happen basically automatically. If it isn't, something isn't setup right.
  • 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")
    
    Thorbjørn Ravn Andersen : There are many EBCDIC code tables. It is very tedious to get right manually.
  • why would you write it if java already supports this charset?

0 comments:

Post a Comment