REBOL offers enbase and debase for conversions between base 10 and bases 64, 16, and 2.
That omits base-8 -- octal -- which is a useful one. And it omits all other bases.
This set of functions corrects that by offering (out of the box) conversions to any base up to base-36. (See Customisation for handling bases larger than 36).
do %base-convert.r
base-convert/to-base 99 2 ;; 99 in binary == "1100011" base-convert/to-base 999 8 ;; 999 in octal == "1747" base-convert/to-base 14600926 16 ;; large number in hex == "DECADE" base-convert/to-base 1024 29 ;; 1024 in base 29 == "169" base-convert/to-base 177 "#@" ;; 177 in base-2 using "#" and "@" rather than 0 and 1 == "@#@@###@"
base-convert/from-base "4444" 5 ;;"4444" in base 5 == 624 base-convert/from-base "1000000000000000000000001" 2 == 16777217 base-convert/from-base "AA" 31 ;; "AA" in base 31 (310 + 10) == 320
There are two functions for general public use:
Call format is:
base-convert/to-base decimal base
is the number you want to convert
is the base you want it in. You can specify this in two ways:
The is more on the base options in the Customisation options.
You get a string back
Call format is:
base-convert/from-base number base
is a string containing the number you want to convert
is the base it is in. See base-convert/to-base for specifying this.
You get a decimal back
If you want upper- and lower-case letters to be different digits, set:
base-convert/case-sensitive: true
do %base-convert.r base-convert/to-base 100 "oO" User Error: Base contains duplicate characters: OO But: base-convert/case-sensitive: true base-convert/to-base 100 "oO" =="OOooOoo" ;; 100 in base-2 using O and o rather than 0 and 1
If you want a different default sequence of digits, or you want more than base-36, set:
base-convert/default-digits: "...your digits..."
base-convert/default-digits: "QWERTY" base-convert/to-base 100 3 ;; base-3 using Q, W and E == "WQEQW" base-convert/to-base 100 7 ;; sadly, max base is now length? "QWERTY" User Error: Maximum base should be 6 letters: copy "abcdefghijklmnopqrstuvwxyz" digits: "0123456789" base-convert/default-digits: join digits [letters uppercase copy letters] base-convert/case-sensitive: true base-convert/to-base 100 62 ;; now use up to base 62 == "1C"
The largest number that can safely be used in convert-base/to-base is the largest decimal that can be divided by the base and give an accurate remainder.
That will vary by base, and I'm happy to admit that I haven't researched it or done any testing. Also, as it will fall at the very end of the range of what is possible, it may vary slightly between platforms and versions of REBOL.
You can adjust the maximum number the to-base function will attempt:
convert-base/maximum-decimal: ...a big value...
But be warned, you'll get no support or sympathy if it goes wrong.
If you want to extend, expand, enhance or just fix this script, please do so.
A test driver is included so you can run some verification tests that your changes haven't broken anything obvious. Example:
do %base-convert.r base-convert/test-drive 22:22:08 Tests completed: 0 22:22:10 Tests completed: 1000 22:22:11 Tests completed: 2000 22:22:12 Tests completed: 3000 22:22:13 Tests completed: 4000 22:22:15 Tests completed: 5000 22:22:16 Tests completed: 6000
That's 24,000 conversions (4 * 6000: decimal --> base-x --> decimal. And then base-y --> decimal --> base-y) without a failure. Could mean the last bug will be hard to find.
end