Sto avendo un file flat che viene estratto da una tabella Db2, il file flat contiene i record sia nel formato char sia nel formato decimale compresso. Come per convertire i dati compressi in una stringa java. C'è un modo per convertire l'intero file flat in formato ASCII.convertire la stringa EBCDIC in formato ASCII?
risposta
Leggere il file come stringa, scriverlo come EBCDIC. Utilizzare OutputStreamWriter e InputStreamWriter e assegnare la codifica nel costruttore.
EBCDIC è una famiglia di codifiche. Avrai bisogno di sapere di più in dettaglio quale codifica EBCDIC stai cercando.
Java ha un certo numero di supported encodings, tra cui:
- IBM500/CP500 - 500V1 EBCDIC
- x-IBM834/Cp834 - IBM EBCDIC DBCS solo coreano (double-byte)
- IBM1047/Cp1047 - Set di caratteri Latin-1 per host EBCDIC
Prova quelli e guarda cosa ottieni. Qualcosa di simile:
InputStreamReader rdr = new InputStreamReader(new FileInputStream(<your file>), java.nio.Charset.forName("ibm500"));
while((String line = rdr.readLine()) != null) {
System.out.println(line);
}
In seguito a PAP, CP037 è la codifica EBCDIC statunitense.
Anche dare un'occhiata al progetto JRecord. Ti permette di leggere un file con una descrizione Cobol o Xml e gestirà EBCDIC e Comp-3.
Infine ecco un routine per convertire i byte decimali imballati per String vedere metodo getMainframePackedDecimal in Conversion
La condivisione di un codice di esempio da me per il vostro riferimento:
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);
}
}
Penso che sarebbe più utile per l'OP e altri visitatori, quando aggiungerai qualche spiegazione alla tua intenzione. – reporter
- 1. Disimballaggio Decimali imballati EBCDIC (COMP-3) in una conversione ASCII
- 2. come posso convertire la stringa unicode in ASCII in java
- 3. Come convertire IDN in ASCII?
- 4. da UTF-8 a EBCDIC in Java
- 5. Come convertire la stringa in formato datetime in pandas python?
- 6. Converti da stringa ASCII codificata in Hex in ASCII normale?
- 7. Convertire una stringa ascii letterale in & [u8] in Rust?
- 8. Convertire una stringa di esadecimale in ASCII in Java
- 9. Come convertire la stringa di formato standard .NET in stringa di formato Excel
- 10. data di sql convertire in formato stringa
- 11. convertire la stringa nel formato java.util.date in java
- 12. Come convertire la data in formato word?
- 13. come convertire ascii in unsigned int
- 14. Convertire la stringa fino alla data, formato: "gg.mm.aaaa"
- 15. Converti byte ASCII [] in stringa
- 16. Bash: Converti caratteri non ASCII in ASCII
- 17. convertire la stringa in data tipo python
- 18. Come convertire il formato MetaStock in CSV?
- 19. In python, come convertire una stringa ascii hex in una stringa binaria interna raw?
- 20. Qualunque strumento online per convertire istruzioni SQL in formato stringa?
- 21. Come posso convertire la stringa in datetime con la specifica del formato in JavaScript?
- 22. Python - Comprimi stringa Ascii
- 23. Convertire un elenco di stringa in formato JSON
- 24. convertire una stringa in un formato di data
- 25. Come posso convertire ascii esteso su System.String?
- 26. Come convertire un valore ASCII in un carattere in .NET
- 27. Come convertire un carattere ASCII nel suo valore int ASCII?
- 28. convertire tiff in formato jpg
- 29. Unicode, UTF, ASCII, ANSI differenze di formato
- 30. convertire pdf in formato svg
È da notare che questo lista è delle codifiche supportate da * Oracle JDK *. L'elenco di codifiche [richiesto per essere supportato da ** all ** JVMs è molto più breve] (http://download.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html#standard). –
Ottimo punto! – pap