C2D (Character to Decimal)

The C2D function returns the decimal value of the binary representation of string.

Read syntax diagramSkip visual syntax diagram C2D( string ,n )

If the result cannot be expressed as a whole number, an error results. That is, the result must not have more digits than the current setting of NUMERIC DIGITS. If you specify n, it is the length of the returned result. If you do not specify n, string is processed as an unsigned binary number.

If string is null, returns 0.

Implementation maximum: The input string cannot have more than 250 characters that are significant in forming the final result. Leading sign characters ('00'x and 'FF'x) do not count toward this total.

Examples

C2D('09'X)      ->        9
C2D('81'X)      ->      129
C2D('FF81'X)    ->    65409
C2D('')         ->        0
C2D('a')        ->      129     /*  EBCDIC  */

If you specify n, the string is taken as a signed number expressed in n characters. The number is positive if the leftmost bit is off, and negative, in two's complement notation, if the leftmost bit is on. In both cases, it is converted to a whole number, which may, therefore, be negative. The string is padded on the left with '00'x characters (note, not “sign-extended”), or truncated on the left to n characters. This padding or truncation is as though RIGHT(string,n,'00'x) had been processed. If n is 0, C2D always returns 0.

C2D('81'X,1)      ->     -127
C2D('81'X,2)      ->      129
C2D('FF81'X,2)    ->     -127
C2D('FF81'X,1)    ->     -127
C2D('FF7F'X,1)    ->      127
C2D('F081'X,2)    ->    -3967
C2D('F081'X,1)    ->     -127
C2D('0031'X,0)    ->        0