Example: converting to and from national data
The following example shows the NATIONAL-OF
and DISPLAY-OF
intrinsic functions and the MOVE
statement for converting
to and from national (UTF-16) data items. It also demonstrates the
need for explicit conversions when you operate on strings that are
encoded in multiple code pages.
CBL CODEPAGE(00037)
* . . .
01 Data-in-Unicode pic N(100) usage national.
01 Data-in-Greek pic X(100).
01 other-data-in-US-English pic X(12) value "PRICE in $ =".
* . . .
Read Greek-file into Data-in-Greek
Move function National-of(Data-in-Greek, 00875)
to Data-in-Unicode
* . . . process Data-in-Unicode here . . .
Move function Display-of(Data-in-Unicode, 00875)
to Data-in-Greek
Write Greek-record from Data-in-Greek
The example above works correctly because the input
code page is specified. Data-in-Greek
is converted
as data represented in CCSID 00875 (Greek). However,
the following statement results in an incorrect conversion unless
all the characters in the item happen to be among those that have
a common representation in both the Greek and the English code pages:
Move Data-in-Greek to Data-in-Unicode
The MOVE
statement
above converts Data-in-Greek
to Unicode representation
based on the CCSID 00037 (U.S. English) to UTF-16 conversion. This
conversion does not produce the expected results because Data-in-Greek
is
encoded in CCSID 00875.
If you can correctly set
the CODEPAGE
compiler option to CCSID 00875 (that
is, the rest of your program also handles EBCDIC data in Greek), you
can code the same example correctly as follows:
CBL CODEPAGE(00875)
* . . .
01 Data-in-Unicode pic N(100) usage national.
01 Data-in-Greek pic X(100).
* . . .
Read Greek-file into Data-in-Greek
* . . . process Data-in-Greek here ...
* . . . or do the following (if need to process data in Unicode):
Move Data-in-Greek to Data-in-Unicode
* . . . process Data-in-Unicode
Move function Display-of(Data-in-Unicode) to Data-in-Greek
Write Greek-record from Data-in-Greek