1. Home
  2. NCONVERT procedure

NCONVERT procedure

Converts integers between base 10 and other bases (R.W. Payne).

Options

PRINT = string token Controls printed output (number); default numb
METHOD = string token Whether to convert NUMBER to DIGITS or vice versa (tobase, frombase); default toba
BASE = scalars Base to which to convert number; default 2

Parameters

NUMBER = scalars Number in base 10
DIGITS = pointers Digits of the NUMBER in the base specified by the BASE option
SIGN = scalars Sign of the NUMBER

Description

NCONVERT can be used to convert an integer between the standard base 10 and another base, specified by the BASE option (default 2 i.e. binary). The number in base 10 is specified by the NUMBER parameter. In the other base it is represented in a pointer, specified by the DIGITS parameter, containing an integer for each of the digits required to represent it in that base. The SIGN parameter contains a scalar with the value +1 or -1 according to whether the number is positive or negative.

For example, the number 29 in base 10 would be represented in base 2 by a pointer containing five scalars with the values 1, 1, 1, 0 and 1. This results from the fact that

29 = 16 + 8 + 4 + 1

So there are 5 digits corresponding to the multipliers of 24, 23, 22, 21 and 20.

The PRINT option has a single setting, number, which prints the number in the two bases. By default this is printed, but you can suppress that by setting PRINT=*. The METHOD option controls the direction of the conversion: the default, tobase, converts from base 10 to the other base; conversion in the other direction is requested by the alternative setting, frombase.

Options: PRINT, METHOD, BASE.

Parameters: NUMBER, DIGITS, SIGN.

Method

The conversion is done by standard arithmetic using, for example, the MODULO function.

See also

Procedures: BPCONVERT, PRIMEPOWER.
Commands for: Calculations and manipulation.

Example

CAPTION  'NCONVERT example'; STYLE=meta
NCONVERT 36176
&        -36176; DIGITS=D; SIGN=S
PRINT    D[]; FIELD=3; DECIMALS=1
NCONVERT [METHOD=FROMBASE] *; DIGITS=D; SIGN=S
&        *; DIGITS=D; SIGN=1
NCONVERT [BASE=3] 90
&        -90; DIGITS=D; SIGN=S
PRINT    D[]; FIELD=3; DECIMALS=1
NCONVERT [BASE=3; METHOD=FROMBASE] *; DIGITS=D; SIGN=S
&        *; DIGITS=D; SIGN=1
Updated on January 12, 2022

Was this article helpful?