View script | License | Download documentation as: HTML or editable |

Download script | History | Other scripts by: sunanda |

[0.052] 17.375k

## Documentation for: base-convert.r## 1. Version/History- Sunanda
- 1-aug-2004
- Version: 0.0.1
- License: GPL
## 2. IntroductionREBOL 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 ## 3. Quick examples## 3.1. Loading the function:do %base-convert.r ## 3.2. Converting to other bases from base-10base-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 == "@#@@###@" ## 3.3. Converting from other bases to base-10base-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 ## 4. Functions in detailThere are two functions for general public use: - base-convert/to-base -- takes a decimal and rebases it
- base-convert/from-base -- takes a number in any base and decimalises it
## 4.1. base-convert/to-baseCall format is: base-convert/to-base ## 4.1.1. decimalis the number you want to convert ## 4.1.2. baseis the base you want it in. You can specify this in two ways: - as an integer 2-36 -- meaning bases 2 through 36 respectively
- as a string with the digits you want to use:
- 01234 -- would be the same as 5 for base-5
- XY -- means base-2, but using X for 0 and Y for 1
- 0123456789TE -- base-12 using T and E rather than the default A and B
The is more on the base options in the ## 4.1.3. ReturnsYou get a ## 4.2. base-convert/from-baseCall format is: base-convert/from-base ## 4.2.1. numberis a ## 4.2.2. baseis the base it is in. See ## 4.2.3. ReturnsYou get a ## 5. Customisation## 5.1. Case sensitivityIf you want upper- and lower-case letters to be base-convert/case-sensitive: true ## 5.1.1. exampledo %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 ## 5.2. Different digit stringsIf you want a different default sequence of digits, or you want more than base-36, set: base-convert/default-digits: "...your digits..." ## 5.2.1. Examplesbase-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" ## 5.3. Maximum decimalThe largest number that can 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. ## 6. TestingIf 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 |