Base Conversion

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

decimal->binary

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.

Images should be redone so they are the same size (zoom 1.5). --MF
  1. Write the base7 block, which displays a number in base 7.
    base7 of 9827 reports 40436
  2. Generalize the pattern with a base block that takes the base as a second input:
    9827 base 2 9827 base 7
    9827 base 8 9827 base 10
    32562 base 15 53749006 base 36
  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 in base 10.
    (23143) from base (8)
  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.
  2. Improve the from base block to support bases up to 36 in the same manner.