OCT$ function

Purpose

Return a string that is the octal (base 8) representation of its argument.

Syntax

s$ = OCT$(numeric_expression [, digits])

Remarks

numeric_expression must be in the range -2,147,483,648 to +4,294,967,295.  Any fractional part of numeric_expression is rounded before the string is created.

If digits is specified, the result will be of the length digits.  If the value is shorter than digits, leading zero will be supplied to pad the string.  If the value is longer than digits, the result will be truncated from the left.  digits may range from 1 to 11.  If digits is zero, no padding is used.  If digits is less than zero, PowerBASIC pads the string to 11 digits.

Octal is a number system that uses base 8, rather than the base 10 used by the ordinary decimal system.  A single octal digit represents three bits.  Octal notation was commonly used in programming, because it was a convenient way of representing the binary bit patterns used internally by computers.  Octal has largely been replaced by hexadecimal on modern computers, due to the more convenient size of hexadecimal notation.  However, octal is still often used in Unix-oriented operating systems, and in C code, typically for historical reasons.

By convention, octal numbers are unsigned, since they represent bit patterns.  If you are not familiar with two's-complement arithmetic, the result of using OCT$ on a negatively signed value may surprise you.  See the BIN$ function entry for information about two's-complement arithmetic.

Octal strings can be converted to numeric values with the VAL function by prefixing the string with "&O", "&Q" or just "&".  If the string has a leading zero, the result is always unsigned.  For example:

a$ = OCT$(65535)     ' a$ contains "177777"

x& = VAL("&Q" + a$)  ' Signed result (-1)

y& = VAL("&Q0" + a$) ' Unsigned result (65535)

See also

BIN$, FORMAT$, HEX$, STR$, USING$, VAL