简体   繁体   English

将EBCDIC String转换为ASCII格式?

[英]convert EBCDIC String to ASCII format?

我有一个从Db2表中提取的平面文件,平面文件包含char格式的记录以及压缩十进制格式。将打包数据转换为java字符串。有没有办法转换整个平面文件到ASCII格式。

EBCDIC is a family of encodings. EBCDIC是一系列编码。 You'll need to know more in details which EBCDIC encoding you're after. 您需要详细了解您所追求的EBCDIC编码。

Java has a number of supported encodings , including: Java有许多支持的编码 ,包括:

  • IBM500/Cp500 - EBCDIC 500V1 IBM500 / Cp500 - EBCDIC 500V1
  • x-IBM834/Cp834 - IBM EBCDIC DBCS-only Korean (double-byte) x-IBM834 / Cp834 - IBM EBCDIC仅限DBCS的韩语(双字节)
  • IBM1047/Cp1047 - Latin-1 character set for EBCDIC hosts IBM1047 / Cp1047 - EBCDIC主机的Latin-1字符集

Try those and see what you get. 尝试那些,看看你得到了什么。 Something like: 就像是:

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

Following from PAP, CP037 is US EBCDIC encoding. 从PAP开始,CP037是US EBCDIC编码。

Also have a look at JRecord Project. 还可以看看JRecord Project。 It allows you to read a file with either a Cobol or Xml description and will handle EBCDIC and Comp-3. 它允许您使用Cobol或Xml描述读取文件,并将处理EBCDIC和Comp-3。

Finally here is a routine to convert packed decimal bytes to String see method getMainframePackedDecimal in Conversion 最后,这是一个将压缩十进制字节转换为String的例程,参见转换中的方法getMainframePackedDecimal

Read the file as a String, write it as EBCDIC. 将该文件作为String读取,将其写为EBCDIC。 Use the OutputStreamWriter and InputStreamWriter and give the encoding in the constructor. 使用OutputStreamWriter和InputStreamWriter并在构造函数中提供编码。


Sharing a sample code by me for your reference: 由我分享示例代码供您参考:

    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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM