Base Conversion

MF: I want to review/revise. (potentially into a 1-page lab? maybe not...)

Pages or lab needs renaming so they aren't duplicative. --MF, 6/15/20

On this page, you will generalize from base conversion with binary to conversion with any base.

Here's one solution for binary conversion of non-negative integers:

decimal->binary(number){if(number<2){report(number)}else{report(join(decimal->binary(floor(number/2)))(number mod 2))}}

If a number can be represented with only a single digit, we use it as the base case. For binary, the only numbers that can be represented with a single digit are 0 and 1, so we test to see if the number is less than 2. If so, we report it.

It may be surprising that we don't use an arithmetic combiner, but the desired result is a numeral, a text string. This is why the combiner is a string operation.
In the recursive case, the rightmost binary digit is the remainder when dividing the number by 2. The other digits come from a recursive call on the quotient when dividing by 2. The combiner is join because we want to string the digits together.

  1. Write the base7 block, which displays a number in base 7.
    base7( 9827), reporting 40436
  2. Generalize the pattern with a base block that takes the base as a second input:
    (9827)base(2), reporting 10011001100011 (9827)base(7), reporting 40436
    (9827)base(8), reporting 23143 (9827)base(10), reporting 9827
  3. Snap! will show this number in decimal, but you're converting to a number, not a string of digits.
    Write the inverse function from base that takes a (text) string of digits and a base as inputs, and reports the corresponding number.
    (23143)from base(8) reporting 9827
  1. Improve the base block so that it can go up to base 36 by using the letters az as digits with values 10‒35.
    (32562)base(15), reporting 99ac (53749006)base(36) reporting w00zy
  2. Improve the from base block to support bases up to 36 in the same manner.