我有一個從Db2表中拉出的平面文件,平面文件包含char格式以及壓縮十進制格式的記錄。如何將打包數據轉換爲java string.is有任何轉換方法整個平面文件轉換爲ASCII格式。將EBCDIC字符串轉換爲ASCII格式?
7
A
回答
1
以字符串形式讀取文件,將其寫爲EBCDIC。使用OutputStreamWriter和InputStreamWriter並在構造函數中給出編碼。
16
EBCDIC是一組編碼。您需要詳細瞭解您的EBCDIC編碼細節。
Java有一個號碼的supported encodings,包括:
- IBM500/CP500 - EBCDIC 500V1
- 的x IBM834/Cp834 - IBM EBCDIC DBCS-僅韓語(雙字節)
- 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);
}
1
從PAP之後,CP037是US EBCDIC編碼。
也看看JRecord項目。它允許您使用Cobol或Xml描述讀取文件,並處理EBCDIC和Comp-3。
最後這裏是壓縮十進制字節轉換爲字符串見方法getMainframePackedDecimal在Conversion
-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 = "abcdef".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
我認爲當你爲你的內涵添加一些解釋時,它對OP和更多訪問者會更有幫助。 – reporter
相關問題
- 1. 將字符串從ASCII轉換爲Java中的EBCDIC?
- 2. HEX字符串轉換爲EBCDIC返回數字格式異常
- 3. PHP/SQL - 將EBCDIC轉換爲ASCII
- 4. 將EBCDIC代碼轉換爲ASCII
- 5. 將字符串轉換爲字符ascii
- 6. 將字符串轉換爲ascii和ascii爲字符串
- 7. 如何將中文字符的EBCDIC轉換爲UTF-8格式
- 8. 將字符串轉換爲ASCII和ASCII字符串
- 9. 如何在JavaScript中將字符串轉換爲ASCII格式?
- 10. 將字符串轉換爲格式爲
- 11. 將Ascii字符串轉換爲位流
- 12. 將字符串轉換爲ASCII值
- 13. 將字符串轉換爲ASCII
- 14. C++ ::將ASCII值轉換爲字符串
- 15. 將字符串轉換爲ASCII值python
- 16. 將字符串轉換爲ASCII碼
- 17. 將字符串轉換爲ASCII碼
- 18. 將Unicode字符串轉換爲ASCII
- 19. 將字符串轉換爲ASCII值
- 20. PHP - 轉換EBCDIC到ASCII
- 21. 將EBCDIC字符轉換爲十六進制值(AFP EBCDIC數據)
- 22. Bash:將非ASCII字符轉換爲ASCII
- 23. 將字符串從EBCDIC轉換爲Unicode/UTF8
- 24. 將ASCII字符串轉換爲USB UTF16字符串格式表示爲使用shell命令的shell字符串
- 25. 將ASCII字符轉換爲十六進制轉義字符串
- 26. 如何將utf8字符串轉換爲ascii字符串?
- 27. Python:將ascii字符串轉換爲unicode字符串
- 28. Java:如何將ASCII字符串轉換爲字符串?
- 29. 將字符轉換爲ASCII字符
- 30. 擴展ASCII字符串轉換爲十六進制格式
應當指出的是,這列表是由Oracle JDK *支持的編碼。 [所有** JVM都需要支持的編碼列表要短得多](http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html#standard )。 –
非常好的一點! – pap