将EBCDIC格式的字符串转换为ASCII格式?

7

我有一个从Db2表中提取的平面文件,该文件包含字符格式和压缩十进制格式的记录。如何将压缩数据转换为Java字符串?是否有一种方法将整个平面文件转换为ASCII格式。

4个回答

16

EBCDIC 是一系列编码方式。您需要更详细地了解您需要的是哪种 EBCDIC 编码方式。

Java 支持多种编码方式,包括:

  • IBM500/Cp500 - EBCDIC 500V1
  • x-IBM834/Cp834 - 仅双字节的 IBM EBCDIC 韩语编码
  • IBM1047/Cp1047 - 适用于 EBCDIC 主机的 Latin-1 字符集

您可以尝试使用这些编码方式以查看结果,例如:

InputStreamReader rdr = new InputStreamReader(new FileInputStream(<your file>), java.nio.Charset.forName("ibm500"));
    while((String line = rdr.readLine()) != null) {
        System.out.println(line);
}

5
需要注意的是,这个列表列出的是Oracle JDK支持的编码格式。而要求所有JVM支持的编码格式列表则要短得多,可以在此链接中查看:http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html#standard。 - Joachim Sauer

1

将文件读取为字符串,以EBCDIC格式写入。使用OutputStreamWriter和InputStreamWriter,并在构造函数中指定编码。


1

继承自PAP,CP037是美国EBCDIC编码。

同时看一下JRecord项目。它允许您使用Cobol或Xml描述读取文件,并处理EBCDIC和Comp-3。

最后,这里有一个将压缩的十进制字节转换为字符串的例程,请参见Conversion中的getMainframePackedDecimal方法。


-1


这是我分享给你的一个示例代码,供你参考:

    package mypackage;
    import java.io.UnsupportedEncodingException;
    import java.math.BigInteger;
    public class EtoA {

public static void main(String[] args) throws UnsupportedEncodingException {

    System.out.println("########");
    String edata = "/ÂÄÀ"; //Some EBCDIC string ==> here the OP can provide the content of flat file which the OP pulled from DB2 table 
    System.out.println("ebcdic source to ascii:");
    System.out.println("ebcdic: " + edata);
    String ebcdic_encoding = "IBM-1047"; //Setting the encoding in which the source was encoded
    byte[] result = edata.getBytes(ebcdic_encoding); //Getting the raw bytes of the EBCDIC string by mentioning its encoding
    String output = asHex(result); //Converting the raw bytes into hexadecimal format
    byte[] b = new BigInteger(output, 16).toByteArray(); //Now its easy to convert it into another byte array (mentioning that this is of base16 since it is hexadecimal)
    String ascii = new String(b, "ISO-8859-1"); //Now convert the modified byte array to normal ASCII string using its encoding "ISO-8859-1"
    System.out.println("ascii: " + ascii); //This is the ASCII string which we can use universally in JAVA or wherever 

    //Inter conversions of similar type (ASCII to EBCDIC) are given below:
    System.out.println("########");
    String adata = "abcd";
    System.out.println("ascii source to ebcdic:");
    System.out.println("ascii: " + adata);
    String ascii_encoding = "ISO-8859-1";
    byte[] res = adata.getBytes(ascii_encoding);
    String out = asHex(res);
    byte[] bytebuff = new BigInteger(out, 16).toByteArray();
    String ebcdic = new String(bytebuff, "IBM-1047");
    System.out.println("ebcdic: " + ebcdic);

    //Converting from hexadecimal string to EBCDIC if needed
    System.out.println("########");
    System.out.println("hexcode to ebcdic");
    String hexinput = "81828384"; //Hexadecimal which we are converting to EBCDIC
    System.out.println("hexinput: " + hexinput);
    byte[] buffer = new BigInteger(hexinput, 16).toByteArray();
    String eout = new String(buffer, "IBM-1047");
    System.out.println("ebcdic out:" + eout);

    //Converting from hexadecimal string to ASCII if needed
    System.out.println("########");
    System.out.println("hexcode to ascii");
    String hexin = "61626364";
    System.out.println("hexin: " + hexin);
    byte[] buff = new BigInteger(hexin, 16).toByteArray();
    String asciiout = new String(buff, "ISO-8859-1");
    System.out.println("ascii out:" + asciiout);
}

//This asHex method converts the given byte array to a String of Hexadecimal equivalent
public static String asHex(byte[] buf) {
    char[] HEX_CHARS = "0123456789abcdef".toCharArray();
    char[] chars = new char[2 * buf.length];
    for (int i = 0; i < buf.length; ++i) {
        chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
        chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
    }
    return new String(chars);
}
}

1
我认为如果您在意图上添加一些解释,对于原帖作者和其他访问者会更有帮助。 - Reporter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接