Encodings: EBCDIC, ASCII and UTF-8

EBCDIC vs ASCII

Mainframes store text in EBCDIC, where letters sit in different code positions than ASCII. Files that cross between a mainframe and Linux need a conversion step, or the text arrives as punctuation soup.

01 WS-NAME PIC X(30). MOVE "Ada" TO WS-NAME.

GnuCOBOL encodings

GnuCOBOL copies source bytes unchanged into PIC X fields. Some builds add an -fencoding option, but this one was built without iconv, so there is no such flag: keep source and data in one encoding and convert files outside the compiler with iconv. A UTF-8 literal already works as it stands:

MOVE "Привет" TO WS-NAME. DISPLAY "Name: " FUNCTION TRIM(WS-NAME).

cobc -x -free -o utf8 utf8.cob

./utf8

Name: Привет

UTF-8 files

PIC X fields are byte-oriented, so one UTF-8 character can span several X positions. For multi-byte text, size fields generously and avoid INSPECT on characters that may be split across a field boundary.

The UTF-8 example on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container. The compiler build has no iconv support, so no EBCDIC conversion was run.

Article author: Arthur Isaev