Learn R Programming

cgwtools (version 3.3)

base2base: Function to convert any base to any other base (up to 36).

Description

For a named vector or value, specify the input and output bases and convert.

Usage

base2base(x,frombase=10, tobase=2)

Value

The equivalent value of x in the new base. If x is a list, a matching list is returned.

Arguments

x

A value of class numeric, bigz, or character. Alternatively, a list containing such values. See Details for various limitations

frombase

An integer from 2 thru 36 indicating the base of x.

tobase

An integer from 2 thru 36 indicating the desired base of the returned value.

Author

Carl Witthoft, carl@witthoft.com

Details

If x has more than one value (e.g. a vector of hex characters) they will be collapsed into a single value. If x is a list, then this is done to each list entry in turn. For hexadecimals, i.e. when frombase is set to 16, any leading "0x" will be stripped for you. The standard alpha "encoding" for digits in bases 11 thru 36 is assumed, i.e. 0-9,a-z .

Keep in mind that builtin functions such as strtoi will be much faster for base conversions supported by those functions.

See Also

Examples

Run this code
base2base('101011',2,10)
base2base(101011,2,10)
base2base('4r8s',30,10)
base2base('101011',12,5)


Run the code above in your browser using DataLab