Skip to content

Instantly share code, notes, and snippets.

@rdingwall
Last active November 11, 2016 09:37
Show Gist options
  • Save rdingwall/2258ff89548fcc913b6b73310cf1a74f to your computer and use it in GitHub Desktop.
Save rdingwall/2258ff89548fcc913b6b73310cf1a74f to your computer and use it in GitHub Desktop.
ASCII to IBM 1047 EBCDIC Go mapping table generator
import org.jpos.iso.ISOUtil;
import sun.nio.cs.ext.IBM1047;
import sun.nio.cs.ext.IBM1140;
import java.awt.event.KeyEvent;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
/**
* Created by richard on 10/11/2016.
*/
public class Foo {
public static void main(String [] args) {
ibm1047();
}
public static void ibm1047() {
IBM1047 cp = new IBM1047();
System.out.println(String.format("ASCII to EBCDIC"));
for (int i = 0; i <= 255; i++) {
byte[] x = new byte[]{(byte)i};
CharBuffer cb = cp.decode(ByteBuffer.wrap(x));
char c = cb.array()[0];
if (isPrintableChar(c)) {
System.out.println(String.format("'%c': 0x%02X,", c, x[0]));
} else {
System.out.println(String.format("'\\u%04X': 0x%02X,", (int)c, x[0]));
}
}
System.out.println(String.format("EBCDIC to ASCII"));
for (int i = 0; i <= 255; i++) {
byte[] x = new byte[]{(byte)i};
CharBuffer cb = cp.decode(ByteBuffer.wrap(x));
char c = cb.array()[0];
if (isPrintableChar(c)) {
System.out.println(String.format("0x%02X: '%c',", i, c));
} else {
System.out.println(String.format("0x%02X: '\\u%04X',", i, (int)c));
}
}
}
public static void ibm1140() {
IBM1140 cp = new IBM1140();
System.out.println(String.format("ASCII to EBCDIC"));
for (int i = 0; i <= 255; i++) {
byte[] x = new byte[]{(byte)i};
CharBuffer cb = cp.decode(ByteBuffer.wrap(x));
char c = cb.array()[0];
if (isPrintableChar(c)) {
System.out.println(String.format("'%c': 0x%02X,", c, x[0]));
} else {
System.out.println(String.format("'\\u%04X': 0x%02X,", (int)c, x[0]));
}
}
System.out.println(String.format("EBCDIC to ASCII"));
for (int i = 0; i <= 255; i++) {
byte[] x = new byte[]{(byte)i};
CharBuffer cb = cp.decode(ByteBuffer.wrap(x));
char c = cb.array()[0];
if (isPrintableChar(c)) {
System.out.println(String.format("0x%02X: '%c',", i, c));
} else {
System.out.println(String.format("0x%02X: '\\u%04X',", i, (int)c));
}
}
}
public static boolean isPrintableChar( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment