Product Site

5.1.7.31. c2d

Returns the decimal value of the binary representation of the receiving 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, the receiving string is processed as an unsigned binary number. If the receiving string is null, C2D returns 0.
Example 5.69. String class — c2d method
Say "09"X~c2d       -->        9
Say "81"X~c2d       -->      129
Say "FF81"X~c2d     -->    65409
Say ""~c2d          -->        0
Say "a"~c2d         -->       97     /*  ASCII   */

If you specify n, the receiving 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 therefore be negative. The receiving 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 receiving_string~right(n,'00'x) had been processed. If n is 0, c2d always returns 0.
Example 5.70. String class — c2d method
Say "81"X~c2d(1)      -->     -127
Say "81"X~c2d(2)      -->      129
Say "FF81"X~c2d(2)    -->     -127
Say "FF81"X~c2d(1)    -->     -127
Say "FF7F"X~c2d(1)    -->      127
Say "F081"X~c2d(2)    -->    -3967
Say "F081"X~c2d(1)    -->     -127
Say "0031"X~c2d(0)    -->        0