base2dec: Convert a string of numeral digits from some base into...

View source: R/num_fun.R

base2decR Documentation

Convert a string of numeral digits from some base into decimal notation.

Description

base2dec converts a sequence of numeral symbols (digits) from its notation as positional numerals (with some base or radix) into standard decimal notation (using the base or radix of 10).

Usage

base2dec(x, base = 2)

Arguments

x

A (required) sequence of numeric symbols (as a character sequence or vector of digits).

base

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

Details

The individual digits provided in x (e.g., from "0" to "9", "A" to "F") must be defined in the specified base (i.e., every digit value must be lower than the base or radix value). See base_digits for the sequence of default digits.

base2dec is the complement of dec2base.

Value

An integer number (in decimal notation).

See Also

dec2base converts decimal numbers into numerals in another base; as.roman converts integers into Roman numerals.

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

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

Examples

# (a) single string input:
base2dec("11")   # default base = 2
base2dec("0101")
base2dec("1010")

base2dec("11", base = 3)
base2dec("11", base = 5)
base2dec("11", base = 10)

base2dec("11", base = 12)
base2dec("11", base = 14)
base2dec("11", base = 16)

# (b) numeric vectors as inputs:
base2dec(c(0, 1, 0))
base2dec(c(0, 1, 0), base = 3)

# (c) character vector as inputs:
base2dec(c("0", "1", "0"))
base2dec(c("0", "1", "0"), base = 3)

# (d) multi-digit vectors:
base2dec(c(1, 1))
base2dec(c(1, 1), base = 3)

# Extreme values:
base2dec(rep("1", 32))          # 32 x "1"
base2dec(c("1", rep("0", 32)))  # 2^32
base2dec(rep("1", 33))          # 33 x "1"
base2dec(c("1", rep("0", 33)))  # 2^33
 
# Non-standard inputs:
base2dec("  ", 2)      # no non-spaces: NA
base2dec(" ?! ", 2)    # no base digits: NA
base2dec(" 100  ", 2)  # remove leading and trailing spaces
base2dec("-  100", 2)  # handle negative inputs (value < 0)
base2dec("- -100", 2)  # handle double negations
base2dec("---100", 2)  # handle multiple negations

# Special cases:
base2dec(NA)
base2dec(0)
base2dec(c(3, 3), base = 3)  # Note message!

# 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.