Start of change

%UINT built-in function

%UINT converts character, logical, decimal, or integer data to unsigned integer format. The converted value can be assigned to a CL variable, passed as a numeric constant to another program or procedure, or specified as a value for a command parameter of a CL command run from compiled CL.

The %UINT built-in function can be used anywhere that CL supports an arithmetic expression. %UINT can be used alone or as part of a more complex arithmetic expression. For example, %UINT can be used to compare a decimal CL variable to an unsigned integer CL variable in the COND parameter of an IF or WHEN command. %UINT can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *DEC, *INT2, *INT4, *UINT2, or *UINT4.

The format of the convert to unsigned integer data built-in function is:
%UINT(convert-argument)
It can also be formatted as:
%UNS(convert-argument)

The convert-argument must be a CL variable with TYPE of *CHAR, *LGL, *DEC or *INT.

If the convert-argument parameter is a character variable, the following rules apply:
  • The sign is optional. It can only be '+'. It can precede or follow the numeric data.
  • The decimal point is optional. It can be either a period or a comma.
  • Leading and trailing blanks are allowed in the data. For example, ' +3 ' is a valid parameter.
  • All-blank value will return a zero value.
  • If invalid numeric data is found, an exception occurs with CPF0818.

The result will be in unsigned integer format, any decimal digits are truncated without rounding. And the size of the result is always 4 bytes.

The following are examples of using the %UINT built-in function:

Note: *%UNS is allowed for compatibility with RPG but code examples will use %UINT.
  • Convert character variable
    DCL VAR(&POINTS) TYPE(*CHAR) LEN(10) VALUE('+123.45')
    DCL VAR(&ANSWER) TYPE(*UINT)
    
    /* &ANSWER will have the value 223 */
    CHGVAR VAR(&ANSWER) VALUE(100 + %UINT(&POINTS))
  • Convert logical variable
    DCL VAR(&ANSWER1) TYPE(*LGL) VALUE('1')
    DCL VAR(&ANSWER2) TYPE(*LGL) VALUE('1')
    DCL VAR(&NUM) TYPE(*UINT)
    
    /* &NUM will have the value 2. */
    CHGVAR VAR(&NUM) VALUE(%UINT(&ANSWER1) + %UINT(&ANSWER2))
    SNDPGMMSG MSG('The number of YES answers is' *BCAT %CHAR(&NUM)) 
  • Convert packed decimal variable
    DCL VAR(&POINTS1) TYPE(*DEC) LEN(5 2) VALUE(100.23)
    DCL VAR(&POINTS2) TYPE(*DEC) LEN(5 2) VALUE(100.45)
    
    IF (%UINT(&POINTS1) *EQ %UINT(&POINTS2)) +
    THEN(SNDPGMMSG ('The scores are the same!'))
  • Convert integer variable
    DCL VAR(&P1) TYPE(*UINT) LEN(2)
    DCL VAR(&P2) TYPE(*INT) LEN(2) VALUE(1)
    
    CHGVAR VAR(&P1) VALUE(%UINT(&P2))
  • Decimal digits will be truncated without rounding
    DCL VAR(&STRING) TYPE(*CHAR) LEN(10) VALUE('+123.9')
    DCL VAR(&ANSWER) TYPE(*UINT)
    
    /* &ANSWER will have the value 123 */
    CHGVAR VAR(&ANSWER) VALUE(%UINT(&STRING))
End of change