Converting EBCDIC To ASCII In Java


Answer :

If I am interpreting this format correctly you have a binary file format with fixed-length records. Some of these records are not character data (COBOL computational fields?)



So, you will have to read the records using a more low-level approach processing individual fields of each record:



import java.io.*;

public class Record {
private byte[] kdgex = new byte[2]; // COMP
private byte[] b1code = new byte[2]; // COMP
private byte[] b1number = new byte[8]; // DISPLAY
// other fields

public void read(DataInput data) throws IOException {
data.readFully(kdgex);
data.readFully(b1code);
data.readFully(b1number);
// other fields
}

public void write(DataOutput out) throws IOException {
out.write(kdgex);
out.write(b1code);
out.write(b1number);
// other fields
}
}


Here I've used byte arrays for the first three fields of the record but you could use other more suitable types where appropriate (like a short for the first field with readShort.) Note: my interpretation of the field widths is likely wrong; it is just an example.



The DataInputStream is generally used as a DataInput implementation.



Since all characters in the source and target encodings use a one-octet-per code point you should be able to transcode the character data fields using a method like this:



public static byte[] transcodeField(byte[] source, Charset from, Charset to) {
byte[] result = new String(source, from).getBytes(to);
if (result.length != source.length) {
throw new AssertionError(result.length + "!=" + source.length);
}
return result;
}


I suggest tagging your question with COBOL (assuming that is the source of this format) so that someone else can speak with more authority on the format of the data source.



I also faced same issue like converting EBCDIC to ASCII string.
Please find the code below to convert a single EBCDIC to ASCII string.



public class EbcdicConverter
{
public static void main(String[] args)
throws Exception
{
String ebcdicString =<your EBCDIC string>;
// convert String into InputStream
InputStream is = new ByteArrayInputStream(ebcdicString.getBytes());
ByteArrayOutputStream baos=new ByteArrayOutputStream();

int line;
while((line = is.read()) != -1) {
baos.write((char)line);
}
String str = baos.toString("Cp500");
System.out.println(str);
}
}


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable