Product SiteDocumentation Site

7.4.10. C2D (Character to Decimal)


>>-C2D(string--+----+--)---------------------------------------><
               +-,n-+

Returns the decimal value of the binary representation of string. 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, 0 is returned.
Here are some examples:

Example 7.15. Builtin function C2D

C2D("09"X)      ->        9
C2D("81"X)      ->      129
C2D("FF81"X)    ->    65409
C2D("")         ->        0
C2D("a")        ->       97     /*  ASCII   */

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 if the leftmost bit is on. In both cases, it is converted to a whole number, which can be negative. The string is padded on the left with "00"x characters (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.
Here are some examples:

Example 7.16. Builtin function C2D

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