Skip to content

Step 1 of 5 · Reading · ~4 min

Read

The Basics

The 64 Characters

This lesson gives you the two lookups the course is built on: a number from 0 to 63 turned into a character, and a character turned back into its number. Lesson 3 makes those numbers and lesson 5 uses them; you need neither yet.

How it works

Base64 re-spells data — any file's raw contents, text or image alike — with a fixed set of 64 characters. A computer stores everything as bits, and a bit is a single digit, 0 or 1. Each base64 character stands for exactly 6 bits, and six 0-or-1 digits can be written 2 x 2 x 2 x 2 x 2 x 2 = 64 ways, one per value from 0 to 63. The alphabet lists those 64 in order, so a character's position in it IS its value.

The string, index 0 first:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

Four runs (a run is a stretch of consecutive characters):

index:  0-25   26-51   52-61   62   63
chars:  A-Z    a-z     0-9     +    /

26 + 26 + 10 + 1 + 1 = 64; '/' at position 63 carries the largest value.

Two lookups, inverses:

  • value to character: take the character at that position of the string. 18 gives 'S'.
  • character to value: check the positions in turn until one matches. For 'a': 0 holds 'A', no; 1 holds 'B', no; ... 25 holds 'Z', no; 26 holds 'a', yes, so 'a' is 26. No match by 63 means c is not in the alphabet.

The trap is the joins between runs. Every character also has the number the computer stores it as, its ASCII code: 'A' is 65, 'Z' is 90, 'a' is 97 — seven apart in ASCII, one apart here, 25 and 26. So "value = ASCII code minus 65" is right for A-Z and wrong after it, making VAL a come out 32 instead of 26; a scan of the string records the joins for you.

'=' is deliberately absent: it is padding, filler that marks structure instead of carrying data, so it has no 6-bit value and the lookup rejects it like '@' (lesson 4 says what for).

The algorithm

ALPHABET = the string above

function char_at(index):
    if index < 0 or index > 63: report invalid
    return the character at position index of ALPHABET

function value_of(c):
    for position from 0 to 63:
        if ALPHABET at that position equals c: return position
    report invalid   # covers '=', '-', '_'

for each input line (the starter does this part):
    CHAR n: print char_at(n), or INVALID
    VAL c:  print value_of(c), or INVALID

What the grader runs

Standard input, the text fed to your program, holds zero or more command lines, no blanks: CHAR <n>, n a decimal integer that may be negative, or VAL <c>, c one printable non-space character (letter, digit or punctuation, never a space).

Print one line per command: for CHAR n the character at index n, or INVALID when n is below 0 or above 63; for VAL c the index 0-63 of c, or INVALID when c is not in the alphabet. Some languages let a negative index count backwards from the end of a string instead of failing, so your own range check must reject CHAR -1, not the language.

input:
CHAR 0
CHAR 26
VAL Z
VAL +
VAL =
CHAR 64

output:
A
a
25
62
INVALID
INVALID
input:
CHAR 52
VAL 0
VAL 9
CHAR 63

output:
0
52
61
/

Print exactly this, uppercase INVALID with no reason text, one result per line, newline-terminated, nothing else.

Notes

  • Malformed arguments are handled for you: the starter prints INVALID when CHAR's argument is not a number or VAL's is not one character.
  • VAL = is INVALID (the wrong answers people give here are 0 and 64), and so are VAL - and VAL _, legal only from lesson 7.
  • Hidden tests push both ends of the range (CHAR -1, CHAR 64, CHAR 999) and the ASCII neighbours of the runs — '@', '[', '', '{' — all INVALID`.
  • Empty input produces no output at all, not a blank line. The alphabet is fixed by RFC 4648, the internet standard defining base64: https://www.rfc-editor.org/rfc/rfc4648

Your exercise

Fill in the two lookup functions in the starter: one turning an index into its alphabet character, one turning a character into its index. Each reports failure by returning the value named in that function's own description and again in the comment on its return line — the empty-or-zero character for the index lookup, -1 for the character lookup — so read them for the spelling your language uses. The starter declares the alphabet, dispatches commands and prints INVALID; the mistake caught most often is skipping the range check, so CHAR 64 runs off the end of the string instead of printing INVALID.

Up nextThree Bytes Become Four CharactersEncoding

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…