dec2base: Convert an integer in decimal notation into a string of...

View source: R/num_fun.R

dec2baseR Documentation

Convert an integer in decimal notation into a string of numeric digits in some base.

Description

dec2base converts an integer from decimal (i.e., base 10) notation into a sequence of numeric symbols (digits) of some other base.

Usage

dec2base(x, base = 2, as_char = TRUE)

Arguments

x

A (required) integer in decimal (base 10) notation or corresponding string of digits (i.e., 0-9).

base

The base of the symbols in seq. Default: base = 2 (binary).

as_char

Return result as character string? Default: as_char = TRUE.

Details

To prevent erroneous interpretations of numeric outputs, dec2base returns a sequence of digits (as a character string). When using as_char = FALSE, its output string is processed by as.integer, but this may cause problems with the interpretation of the numeric value (as outputs for bases other than 10 do NOT denote decimal numbers) and scientific notation.

dec2base is the complement of base2dec.

Value

A string of digits (in base notation).

See Also

base2dec converts numbers from some base into decimal numbers; as.roman converts integers into Roman numerals.

Other numeric functions: base2dec()

Examples

# (a) single numeric input:
dec2base(3)  # base = 2
dec2base(4)
dec2base(8)

dec2base(8, base = 3)
dec2base(8, base = 7)

dec2base(100, base = 2)
dec2base(100, base = 5)
dec2base(100, base = 10)

# (b) single string input:
dec2base("7", base = 2)
dec2base("8", base = 3)

# Note: 
base2dec(dec2base(012340, base = 5), base = 5)
dec2base(base2dec(043210, base = 5), base = 5)


hneth/i2ds documentation built on Aug. 17, 2024, 9:31 p.m.