LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Migrating Java application from HP/UX to Linux - String to byte array issues (https://www.linuxquestions.org/questions/linux-newbie-8/migrating-java-application-from-hp-ux-to-linux-string-to-byte-array-issues-4175457248/)

JayMac2013 04-07-2013 11:59 AM

Migrating Java application from HP/UX to Linux - String to byte array issues
 
I have a Java application which is currently running on an HP/UX box and is being migrated to Suse Linux. The application must translate data from ASCII to EBCDIC to send to a mainframe legacy app. If I have the code line below in in HP/UX, it works fine:

String transactionName = new String(transNameByteArray);

However, executing this same code in Linux produces a String which is neither ASCII nor EBCDIC valid data. I understand that I may need to force the code page used as it may be different on HP/UX than on Linux but I am not sure what the code page would be. The line below doesn't correct the issue:

String transactionName = new String(transNameByteArray, "ISO-8859-1");

ntubski 04-07-2013 01:30 PM

Quote:

However, executing this same code in Linux produces a String which is neither ASCII nor EBCDIC valid data.
A String is Unicode.

ASCII <-> EBCDIC conversion (I used IBM500 because the EBCDIC Wikipedia page uses it in the example):
Code:

import java.math.*;
import java.nio.charset.*;

public class Ascii2Ebcdic {
    public static byte[] conv(Charset from, Charset to, byte[] bytes) {
        return new String(bytes, from).getBytes(to);
    }
    public static void main(String[] args) throws Exception {
        Charset ascii = Charset.forName("US-ASCII");
        Charset ebcdic = Charset.forName("IBM500");

        byte[] abc_ascii = { 0x61, 0x62, 0x63 };
        byte[] xyz_ebcdic = { (byte)0xA7, (byte)0xA8, (byte)0xA9 };
        System.out.write(conv(ascii, ebcdic, abc_ascii));
        System.out.write(conv(ebcdic, ascii, xyz_ebcdic));
    }
}

Output (which is "abc" in EBCDIC followed by "xyz" in ASCII):
Code:

% java Ascii2Ebcdic | od -t x1
0000000 81 82 83 78 79 7a
0000006



All times are GMT -5. The time now is 09:07 AM.