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.
* . . .
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, "ISO8859-7")
to Data-in-Unicode
* . . . process Data-in-Unicode here . . .
Move function Display-of(Data-in-Unicode, "ISO8859-7")
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 ISO8859-7
(Ascii 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
Assuming that the locale in effect is en_US.ISO8859-1, the
MOVE
statement above converts Data-in-Greek
to Unicode based on
the code page ISO8859-1 to UTF-16LE conversion. This conversion does not produce the expected results because
Data-in-Greek
is encoded in ISO8859-7.
If you set the locale to el_GR.ISO8859-7 (that is, your program handles ASCII data in Greek), you can code the same example correctly as follows:
* . . .
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