We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Encoding Bytes

Encoding and encryption are not the same. When we talk about encoding, we are talking about converting raw data (binary) into a format that can be stored or transmitted as text.

There are many popular ways to encode data as text. Some include:

Typically, it is a bad idea to create your own encoding scheme. However, for the sake of learning, we are going to create one for fun.

Interactive example available with JavaScript enabled.

Alphabets

Encoding schemes have an alphabet. An alphabet is just the set of available characters in the scheme.

Arabic Numerals Alphabet

0123456789

Hexadecimal Alphabet

0123456789abcdef

Base64 Alphabet

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

The number of characters in the alphabet is the "base". Base64 has 64 characters.

Binary Literals in Go

Binary integer literals are written in Go as follows:

// zeroWrittenInBinary is just an int with the value 0
zeroWrittenInBinary := 0b0000
fmt.Println(zeroWrittenInBinary)
// prints: 0

// oneWrittenInBinary is just an int with the value 1
oneWrittenInBinary := 0b0001
fmt.Println(oneWrittenInBinary)
// prints: 1

Assignment

Complete the base8Char function. It accepts a single byte of data and returns the associated character from our alphabet: ABCDEFGH.

For example, given these binary numbers, it will return the following characters:

0000 -> A
0001 -> B
0010 -> C
0011 -> D
etc

Note that because we only have 8 characters in our alphabet, we will ignore any numbers equal to or greater than 8. Remember, an entire byte can contain the numbers 0 through 255.

Steps

  1. Convert the byte to an integer using int().
  2. If the number is out of the range of our alphabet, return an empty string.
  3. Return a string containing the character associated with the index represented by the integer.