Intrinsic Functions are available to convert character-string data items to the following:
You can use the NATIONAL-OF and DISPLAY-OF intrinsic functions to convert to and from national (Unicode) strings.
Use TRIM, TRIML TRIMR intrinsic functions to remove leading and/or trailing characters from a string.
Besides using Intrinsic Functions to convert characters, you can also use the INSPECT statement.
This code:
01 ITEM-1 PIC X(30) VALUE "Hello World!". 01 ITEM-2 PIC X(30). . . . DISPLAY ITEM-1. DISPLAY FUNCTION UPPER-CASE(ITEM-1). DISPLAY FUNCTION LOWER-CASE(ITEM-1). MOVE FUNCTION UPPER-CASE(ITEM-1) TO ITEM-2. DISPLAY ITEM-2.
would display the following messages on the terminal:
Hello World! HELLO WORLD! hello world! HELLO WORLD!
The DISPLAY statements do not change the actual contents of ITEM-1 and only affect how the letters are displayed. However, the MOVE statement causes uppercase letters to be moved to the actual contents of ITEM-2.
The following code:
MOVE FUNCTION REVERSE(ORIG-CUST-NAME) TO ORIG-CUST-NAME.
would reverse the order of the characters in ORIG-CUST-NAME. For example, if the starting value were JOHNSON, the value after the statement is performed would be NOSNHOJ.
The NUMVAL and NUMVAL-C functions convert character strings to numbers. Use these functions to convert alphanumeric data items that contain free format character representation numbers to numeric form, and process them numerically. For example:
01 R PIC X(20) VALUE "- 1234.5678". 01 S PIC X(20) VALUE "-$12,345.67CR". 01 TOTAL USAGE IS COMP-2. . . . COMPUTE TOTAL = FUNCTION NUMVAL(R) + FUNCTION NUMVAL-C(S).
The difference between NUMVAL and NUMVAL-C is that NUMVAL-C is used when the argument includes a currency symbol or comma, as shown in the example. You can also place an algebraic sign in front or in the rear, and it will be processed. The arguments must not exceed 18 digits (not including the editing symbols). For exact syntax rules, see the ILE COBOL for AS/400 Reference manual.
When you use NUMVAL or NUMVAL-C, you do not need to statically declare numeric data in a fixed format and input data in a precise manner. For example, for this code:
01 X PIC S999V99 LEADING SIGN IS SEPARATE. . . . ACCEPT X FROM CONSOLE.
the user of the application must enter the numbers exactly as defined by the PICTURE clause. For example:
+001.23 -300.00
However, using the NUMVAL function, you could code:
01 A PIC X(10). 01 B PIC S999V99. . . . ACCEPT A FROM CONSOLE. COMPUTE B = FUNCTION NUMVAL(A).
and the input could be:
1.23 -300
The CONVERT-DATE-TIME function takes an alphanumeric, numeric, or date-time item, and converts it to a date-time data item. The intrinsic functions can be used to:
For example, the following statement converts a non-numeric literal (an alphanumeric constant) to a category date data item:
MOVE FUNCTION CONVERT-DATE-TIME ('98/08/09' DATE '%y/%m/%d')
TO DATE-1.
Conversion also occurs when comparing or moving numeric data items containing dates to date-time data items. For more information about the considerations for these types of moves, refer to MOVE Considerations for Date-Time Data Items.
When moving alphanumeric data items containing dates to date-time data items, no conversion is done: whatever characters are contained in the alphanumeric data item are moved to the date-time data item. It is the responsibility of the programmer to ensure that dates contained in alphanumeric data items are in the correct format before they are moved to date-time data items.
The UTF8STRING function converts character strings to UTF-8 (UCS Transformation Format 8). The UTF-8 coded form is represented by CCSID 1208. For example:
01 STR1 PIC X(3) VALUE "ABC". 01 VRR-X3 PIC X(3). . . . MOVE FUNCTION UTF8STRING(STR1) TO VRR-X3.
The contents of VRR-X3 would become X"414243".
Use the NATIONAL-OF intrinsic function to convert an alphabetic, alphanumeric, or DBCS item to a character string represented in Unicode (UCS-2). Specify the source code page as an argument if the source is encoded in a different code page than is in effect with the CCSID compiler option.
Use the DISPLAY-OF intrinsic function to convert a national item to a character string represented in the code page that you specify as an argument or with the CCSID compiler option. If you specify an EBCDIC code page that combines SBCS and DBCS characters, the returned string might contain a mixture of SBCS and DBCS characters, with DBCS substrings delimited by shift-in and shift-out characters.
Overriding the default code page
In some cases, you might need to convert data to or from a CCSID that differs from the CCSID specified as the CCSID option value. To do this, use a conversion function in which you specify the code page for the item explicitly.
If you specify a code page as an argument to DISPLAY-OF and it differs from the code page that you specify with the CCSID compiler option, do not use the DISPLAY-OF function result in any operations that involve implicit conversion (such as an assignment to, or comparison with, a national data item). Such operations assume the EBCDIC code page that is specified with the CCSID compiler option.
Conversion exceptions
Implicit or explicit conversion between national and alphanumeric data could fail and generate a severity-40 error. Failures could occur if any of the following occur:
A character that does not have a counterpart in the target CCSID does not result in a conversion exception. Such a character is converted to a substitution character of the target code page.
The following example shows the use of the NATIONAL-OF and DISPLAY-OF intrinsic functions and the MOVE statement for converting to and from Unicode strings. It also demonstrates the need for explicit conversions when you operate on strings encoded in multiple code pages in the same program.
PROCESS CCSID(37) *... 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 above example works correctly: Data-in-Greek is converted as data represented in CCSID 00875 (Greek) explicitly. However, the following statement would result in an incorrect conversion (unless all the characters in the item happen to be among those with a common representation in the Greek and the English code pages):
Move Data-in-Greek to Data-in-Unicode
Data-in-Greek is converted to Unicode by this MOVE statement based on the CCSID 00037 (U.S. English) to UCS-2 conversion. This conversion would fail because Data-in-Greek is actually encoded in CCSID 00875.
If you can correctly set the CCSID 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:
PROCESS CCSID(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
The TRIM, TRIML, TRIMR functions remove blanks or specified characters from a string. For example:
01 ADDR. 05 STREET-NO PIC X(5) VALUE "120". 05 STEET-NAME PIC X(50) VALUE "Young Street". 05 CITY PIC X(20) VALUE "Toronto". 05 STATE PIC X(15) VALUE "Ontario". 05 ZIP PIC X(6) VALUE "M1C5D9". 01 ADDRESS-LINE PIC X(80). STRING FUNCTION TRIM(STREET-NO) " " FUNCTION TRIM(STREET-NAME) ", " FUNCTION TRIM(CITY) ", " FUNCTION TRIM(STATE) " " FUNCTION TRIM(ZIP) DELIMITED BY SIZE INTO ADDRESS-LINE. DISPLAY ADDRESS-LINE.
The output would be:
120 Young Street, Toronto, Ontario M1C5D9
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.