Skip to content

Instantly share code, notes, and snippets.

@dankox
Created December 7, 2020 17:31
Show Gist options
  • Save dankox/51efac504f60db31e59832fd2fae3e7f to your computer and use it in GitHub Desktop.
Save dankox/51efac504f60db31e59832fd2fae3e7f to your computer and use it in GitHub Desktop.
ebcdic java program to create "golang-like" byte array for ebcdic conversion
import java.nio.charset.Charset;
public class Ebcdic {
public static void main(String[] args) {
conversionTable("cp1140");
conversionTable("cp1047");
}
public static void conversionTable(String ebcdic) {
System.out.println("Conversion table for: " + ebcdic);
try {
Charset ebcdic_cp = Charset.forName(ebcdic);
Charset ascii_cp = Charset.forName("ISO-8859-1");
System.out.println("ASCII -> EBCDIC");
for (int i = 0; i <= 255; i++) {
if (i > 0 && i % 16 == 0) {
System.out.println(String.format("// 0x%02X..0x%02X", i - 16, i - 1));
}
byte[] x = new byte[] {(byte)i};
byte[] e = new String(x, ascii_cp).getBytes(ebcdic_cp);
System.out.print(String.format("0x%02X, ", e[0]));
}
System.out.println("// 0xF0..0xFF");
System.out.println();
System.out.println("EBCDIC -> ASCII");
for (int i = 0; i <= 255; i++) {
if (i > 0 && i % 16 == 0) {
System.out.println(String.format("// 0x%02X..0x%02X", i - 16, i - 1));
}
byte[] x = new byte[] {(byte)i};
byte[] e = new String(x, ebcdic_cp).getBytes(ascii_cp);
System.out.print(String.format("0x%02X, ", e[0]));
}
System.out.println("// 0xF0..0xFF");
System.out.println();
} catch (Exception e) {
System.out.println("conversion failed: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment