Pretty soon it's going to be 50 years since IBM unveiled its System/360 family of computers. These were the first to use the EBCDIC character set.

To mark the occasion, let's see who can write the shortest program capable of converting "ordinary" text to and from EBCDIC code page 037. We'll be using a translation table from Wikipedia that maps CP037 to a superset of ISO-8859-1:

EBCDIC037_to_Latin1 = [


  1. Your program should take two inputs: (a) a text string, and (b) a flag indicating the operation to be performed.

  2. Based on this flag, your program should either convert each byte of text into the corresponding EBCDIC character, or vice versa.

  3. Input can be obtained from any sensible sources (e.g., command line arguments, stdin, keyboard input), but must not be hard-coded into your program.

  4. Output should be displayed on the screen (e.g., stdout, document.write) or written to a file/pipeline.

  5. Don't use any built-in or external encoding conversion functions (iconv, etc.).

  6. This is a challenge, so the shortest answer (fewest bytes) will win.


(Note: These examples were produced in a terminal configured to use UTF-8 encoding. You may see different results depending on how your system is configured. Hex equivalents are shown for reference only, and don't have to be generated by your code.)

Input: "HELLO WORLD", convert to EBCDIC
Output: "ÈÅÓÓÖ@æÖÙÓÄ" (0xc8c5d3d3d640e6d6d9d3c4)

Input: "ÈÅÓÓÖ@æÖÙÓÄ", convert from EBCDIC

Input: "lower case mostly ends up as gremlins", convert to EBCDIC
Output "" <-- unprintable in utf-8
  • \$\begingroup\$ #5 means for example I can't have a base64-encoded string and to s.decode('base64') to get my look-up table? \$\endgroup\$
    – Claudiu
    Mar 26, 2014 at 21:59
  • \$\begingroup\$ What is "ordinary" text? ASCII? UTF-8? A native String type? \$\endgroup\$
    – intx13
    Mar 26, 2014 at 22:01
  • \$\begingroup\$ Are we converting control codes as well? Or just printable characters? If so, by what rules? \$\endgroup\$
    – intx13
    Mar 26, 2014 at 22:04
  • \$\begingroup\$ @intx13, the translation table is in the question. \$\endgroup\$ Mar 26, 2014 at 22:07
  • \$\begingroup\$ @Claudiu That would be absolutely fine \$\endgroup\$
    – r3mainer
    Mar 26, 2014 at 22:12

1 Answer 1


Bash + tr, 240 bytes


00000000  58 3d 27 9c 09 86 7f 97  8d 8e 0b 2d 13 9d 85 08  |X='........-....|
00000010  87 18 19 92 8f 1c 2d 1f  80 2d 84 0a 17 1b 88 2d  |......-..-.....-|
00000020  8c 05 06 07 90 91 16 93  2d 96 04 98 2d 9b 14 15  |........-...-...|
00000030  9e 1a 20 a0 e2 e4 e0 e1  e3 e5 e7 f1 a2 2e 3c 28  |.. ...........<(|
00000040  2b 7c 26 e9 ea eb e8 ed  ee ef ec df 21 24 2a 29  |+|&.........!$*)|
00000050  3b ac 5c 2d 2f c2 c4 c0  c1 c3 c5 c7 d1 a6 2c 25  |;.\-/.........,%|
00000060  5f 3e 3f f8 c9 ca cb c8  cd ce cf cc 60 3a 23 40  |_>?.........`:#@|
00000070  27 5c 27 27 3d 22 d8 61  2d 69 ab bb f0 fd fe b1  |'\''=".a-i......|
00000080  b0 6a 2d 72 aa ba e6 b8  c6 a4 b5 7e 73 2d 7a a1  |.j-r.......~s-z.|
00000090  bf d0 dd de ae 5e a3 a5  b7 a9 a7 b6 bc bd be 5b  |.....^.........[|
000000a0  5d af a8 b4 d7 7b 41 2d  49 ad f4 f6 f2 f3 f5 7d  |]....{A-I......}|
000000b0  4a 2d 52 b9 fb fc f9 fa  ff 5c 5c f7 53 2d 5a b2  |J-R......\\.S-Z.|
000000c0  d4 d6 d2 d3 d5 30 2d 39  b3 db dc d9 da 9f 27 3b  |.....0-9......';|
000000d0  5b 20 24 31 20 5d 26 26  74 72 20 04 2d ff 20 22  |[ $1 ]&&tr .-. "|
000000e0  24 58 22 7c 7c 74 72 20  22 24 58 22 20 04 2d ff  |$X"||tr "$X" .-.|

Rather trivial solution. Reads from STDIN, prints to STDOUT.

How to use

  • To save the script and make it executable, run these commands:

    base64 -d <<< WD0nnAmGf5eNjgstE52FCIcYGZKPHC0fgC2EChcbiC2MBQYHkJEWky2WBJgtmxQVnhogoOLk4OHj5efxoi48KCt8Junq6+jt7u/s3yEkKik7rFwtL8LEwMHDxcfRpiwlXz4/+MnKy8jNzs/MYDojQCdcJyc9IthhLWmru/D9/rGwai1yqrrmuMaktX5zLXqhv9Dd3q5eo6W3qae2vL2+W12vqLTXe0EtSa309vLz9X1KLVK5+/z5+v9cXPdTLVqy1NbS09UwLTmz29zZ2p8nO1sgJDEgXSYmdHIgBC3/ICIkWCJ8fHRyICIkWCIgBC3/ > ebcdic.sh
    chmod +x ebcdic.sh
  • To convert to EBCDIC (encode), execute the plain command:

  • To convert from EBCDIC (decode), specify any first argument:

    ./ebcdic.sh -d
  • \$\begingroup\$ Nice idea, but I can't get it to work in Darwin or Debian. What system are you running it on? \$\endgroup\$
    – r3mainer
    Mar 27, 2014 at 8:44
  • \$\begingroup\$ I mixed up encoding and decoding. I actually had the direction right in the first version (which uses the inverse of the lookup table), but I somehow managed to delete the double quotes around the variables. Fixed. \$\endgroup\$
    – Dennis
    Mar 27, 2014 at 12:25
  • \$\begingroup\$ Working now :-) \$\endgroup\$
    – r3mainer
    Mar 27, 2014 at 12:38
  • \$\begingroup\$ Could it easily take from the stream in? (the pipe) \$\endgroup\$
    – Sandburg
    Sep 17, 2020 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.