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

View source: R/num_fun.R

dec2baseR Documentation

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

Description

dec2base converts an integer from its standard decimal notation (i.e., using positional numerals with a base or radix of 10) into a sequence of numeric symbols (digits) in some other base.

Usage

dec2base(x, base = 2)

Arguments

x

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

base

The base or radix of the digits in the output. Default: base = 2 (binary).

Details

See base_digits for the sequence of default digits.

To prevent erroneous interpretations of numeric outputs, dec2base returns a sequence of digits (as a character string).

dec2base is the complement of base2dec.

Value

A character string of digits (in base notation).

See Also

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

Other numeric functions: base2dec(), base_digits, is_equal(), is_wholenumber(), num_as_char(), num_as_ordinal(), num_equal()

Other utility functions: base2dec(), base_digits, is_equal(), is_vect(), is_wholenumber(), num_as_char(), num_as_ordinal(), num_equal()

Examples

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

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

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

dec2base(14, base = 14)
dec2base(15, base = 15)
dec2base(16, base = 16)

dec2base(15, base = 16)
dec2base(31, base = 16)
dec2base(47, base = 16)

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

# Extreme values:
dec2base(base2dec(rep("1", 32)))          # 32 x "1"
dec2base(base2dec(c("1", rep("0", 32))))  # 2^32
dec2base(base2dec(rep("1", 33)))          # 33 x "1"
dec2base(base2dec(c("1", rep("0", 33))))  # 2^33

# Non-standard inputs:
dec2base("  ")          # only spaces: NA
dec2base("?")           # no decimal digits: NA
dec2base(" 10 ", 2)     # remove leading and trailing spaces
dec2base("-10", 2)      # handle negative inputs (in character strings)
dec2base(" -- 10", 2)   # handle multiple negations
dec2base("xy -10 ", 2)  # ignore non-decimal digit prefixes

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


ds4psy documentation built on Sept. 15, 2023, 9:08 a.m.