base2base {bigBits} | R Documentation |
Function which converts arbitrary-size integers from any base to any base.
Description
This function accepts inputs in any base from 2 through 36 and produces the same value in any selected base from 2 through 36. This includes options for signed and 2s complement binary data.
Usage
base2base(x,frombase=10, tobase=2, classOut=c('bigz', 'mpfr',
'numeric','character') , binSize = 0, inTwosComp = FALSE,
outTwosComp = FALSE )
Arguments
x |
A value or vector or list of values which are to be converted. The class can generally be numeric, mpfr, bigz, or character strings. Any fractional part is removed, leaving just the integer portion. See Details for more information. |
frombase |
The base of the input |
tobase |
The desired base of the output. Default is 2. |
classOut |
Specify the class of the output. This only has meaning when |
binSize |
Specifies how many digits are to be generated. If this value is less than that necessary to contain the output value, the number of digits will be increased to match. If the output is binary, the final number of digits is expanded to a 4*N value. The default is zero, which allows the function to calculate the minimum bits required. Note: for obvious reasons, this only applies to character-class outputs. |
inTwosComp |
Only checked if |
outTwosComp |
Only checked if |
Details
In general, when submitting an input in other than base 10, it's safest to provide a character string(s). There is some automagical conversion that will take, e.g., a numeric 364
with frombase = 8
and treat as base 8 (thus decimal 244), but this is not guaranteed. Further, keep in mind that numeric values with more than roughly 16 digits will likely run into floating-point precision errors. For base-10 inputs, use of bigz
form is recommended.
Inputs in hex format must be character strings. This is because the command parser converts, e.g., 0x3a, to the decimal value 58 prior to passing the value to the function body. Since, as noted above, base2base
will attempt to convert a numeric input into the value in the base specified, base2base(0x3a, inbase= 16, ...)
will in fact process the input as 58hex, i.e. 88 decimal.
Value
A list containing the converted value(s). Unless tobase
is 10, each element is a character string. When tobase
is equal to 10, the output class is specified with the argument classOut
.
Note: if an input or output is incompatible with the specified input or output base, a dummy value "%no" if character, or "NA" if a number-like class, is returned along with a warning message describing the error.
Author(s)
Author and Maintainer:Carl Witthoft carl@witthoft.com
See Also
strtoi
as.hexmode
fracB2B
Examples
(base2base(12.4e1,10,16))
(base2base(12.4e-2,10,16))
(base2base(101101,2,10)) # magic. it works!!!
(base2base('1111',2,2,inTwosComp=TRUE, outTwosComp=TRUE))
(base2base('0111',2,2,inTwosComp=TRUE, outTwosComp=TRUE))
(base2base('1111',2,2,inTwosComp=TRUE, outTwosComp=FALSE))
(base2base('0111',2,2,inTwosComp=TRUE, outTwosComp= FALSE))
(base2base(1e55,10,16)) #loses precision before even starting
(base2base('1e55',10,16)) #works
(base2base('1767707668033969' , 10, 36))