Page 2: Base Conversion
Unit 8, Lab 2, Page 2
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:

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.
Write the
base7block, which displays a number in base 7.

Generalize the pattern with a
baseblock that takes the base as a second input:


Snap! will show this number in decimal, but you’re converting to a number, not a string of digits.
Write the inverse function
from basethat takes a (text) string of digits and a base as inputs, and reports the corresponding number.

If There Is Time…
Improve the
baseblock so that it can go up to base 36 by using the lettersa‒zas digits with values 10‒35.

Improve the
from baseblock to support bases up to 36 in the same manner.