Text conversion

Character data is converted through the IBM® Toolbox for Java™ AS400Text class. This class converts character data between an EBCDIC code page and character set (CCSID), and Unicode.

When the AS400Text object is constructed, the Java program specifies the length of the string to be converted and the server CCSID or encoding. The CCSID of the Java program is assumed to be 13488 Unicode. The toBytes() method converts from Java form to byte array inIBM i format. The toObject() method converts from a byte array in IBM i format to Java format.

The AS400BidiTransform class provides layout transformations that allow the conversion of bidirectional text in IBM i format (after its conversion to Unicode) to bidirectional text in Java format, or from Java format to IBM i format. The default conversion is based on the CCSID of the job. To alter the direction and shaping of the text, specify a BidiStringType. Note that where IBM Toolbox for Java objects perform the conversion internally, as in the DataArea class, the objects have a method to override the string type. For example, the DataArea class has addVetoableChangeListener() method that you can specify to listen for a veto changes to certain properties, including string type.

Example: Converting text data

The following example assumes that a DataQueueEntry object returns text in EBCDIC. The example converts the EBCDIC data to Unicode, so that the Java program can use data:

     // Assume the data queue work has already been done to
     // retrieve the text from the system and the data has been
     // put in the following buffer.
     int textLength = 100;
     byte[] data = new byte[textLength];

     // Create a converter for the system data type. Note a default
     // converter is being built.  This converter assumes the IBM i
     // EBCDIC code page matches the client's locale.  If this is not
     // true the Java program can explicitly specify the EBCDIC
     // CCSID to use. However, it is recommended that you specify a 
     // CCSID whenever possible (see the Notes: below).
     AS400Text textConverter = new AS400Text(textLength)

     // Note: Optionally, you can create a converter for a specific 
     // CCSID. Use an AS400 object in case the program is running
     // as an IBM Toolbox for Java proxy client.
     int ccsid = 37;
     AS400 system = ...; // AS400 object 
     AS400Text textConverter = new AS400Text(textLength, ccsid, system);

     // Note: You can also create a converter with just the AS400 object.
     // This converter assumes the IBM i code page matches
     // the CCSID returned by the AS400 object.
     AS400Text textConverter = new AS400Text(textLength, system);

     // Convert the data from EBCDIC to Unicode. If the length of
     // the AS400Text object is longer than the number of
     // converted characters, the resulting String will be
     // blank-padded out to the specified length.
     String javaText = (String) textConverter.toObject(data);