What is Base62 Conversion?
Tags: math, go, algorithms, tutorial
Published: 2023-07-27 20:58:06

In today's digital age, we are constantly dealing with large amounts of data, and finding efficient ways to store and process it has become crucial. 

One technique that stands out is base62 conversion - a powerful encoding scheme that allows for compact data storage and is widely used in short URL services. 

In this article, we'll explore the concept of base62 conversion, understand how it works, and see how it can be implemented using Golang.

### What is Base62?

At its core, base62 is an encoding system that derives its name from the characters it uses for encoding. The base62 system employs a set of sixty-two characters, consisting of digits 0-9, lowercase letters a-z, and uppercase letters A-Z. If we break down the character count, we have 10 digits + 26 lowercase letters + 26 uppercase letters, resulting in a total of 62 characters.

### The Conversion Process

To convert a decimal number to base62, we follow a simple process using long division:

1. Divide the decimal number by 62.
2. Record the quotient and keep track of the remainder.
3. Map the remainder to the corresponding base62 character using its position within the base62 digits.

Let's walk through an example to make things clearer. Suppose we want to convert the decimal number 123 to base62:

1. Start by dividing 123 by 62:
   - 123 ÷ 62 = 1 quotient 61 remainder

2. Next, divide 1 by 62:
   - 1 ÷ 62 = 0 quotient 1 remainder

3. Now, we map the remainders to their respective base62 characters. Since the base62 system starts its index at zero, the last character "Z" is at position sixty-one, and "1" is at position one.

Therefore, the decimal number 123 converts to "1Z" in base62.

### Reverse Conversion: Base62 to Decimal

So far, we've looked at converting a decimal number to base62. But what if we want to go the other way around? How do we convert a base62 number back to its decimal equivalent? Let's find out:

1. Identify the position value of each base62 character.
2. Calculate the powers of 62 for each character based on its position.
3. Multiply each character by its corresponding power of 62.
4. Sum up the results to obtain the decimal representation.

Let's demonstrate this process using the base62 number "1Z" we obtained earlier:

1. Determine the position value of each character:
   - "1" is in the 1st position.
   - "Z" is in the 61st position.

2. Calculate the powers of 62:
   - The power of "Z" is determined by using the formula `len(base62) - i - 1` (where `i` represents the position of the digit):
   - For "Z" in position 0, the calculation is `2 - 0 - 1 = 1`.
   - For "1" in position 1, the calculation is `2 - 1 - 1 = 0`.

3. Convert back to decimals:
   - Multiply each digit by its power of 62:
     - 1 * 62^1 = 62
     - Z * 62^0 = 61

5. Sum up the results: 62 + 61 = 123

As we can see, we successfully obtained the original decimal number 123 from the base62 representation "1Z".

### Implementing Base62 Conversion in Golang

Now that we understand how base62 conversion works, let's explore how we can implement it using the Go programming language.

```go
package main

import (
	"fmt"
)

const base62Digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func convertToBase62(number int) string {
	base62 := ""
	for number > 0 {
		remainder := number % 62
		base62 = string(base62Digits[remainder]) + base62
		number /= 62
	}
	return base62
}

func main() {
	decimalNumber := 123
	base62Number := convertToBase62(decimalNumber)
	fmt.Println(base62Number)
}
```

In the above code snippet, we have a `convertToBase62` function that takes a decimal number as input and returns its base62 equivalent. The function uses successive division to calculate the remainders and builds the base62 representation by mapping each remainder to its respective character from the `base62Digits` string. Finally, we divide the number by 62 to continue the division process until the quotient becomes zero. The `main` function demonstrates how to use the `convertToBase62` function by converting the decimal number 123 to base62 and printing the result.

Feel free to run the code on the Go Playground to see it in action: [Go Playground - Base62 Conversion](https://play.golang.com/p/DmFYZXWdzDU)

Base62 conversion provides a straightforward and efficient way to represent numbers using a limited character set. It's particularly useful for applications that require compact data storage, such as short URL services. By leveraging the power of base62, developers can create more concise and user-friendly representations of numbers without sacrificing accuracy.

So, the next time you come across a shortened URL or need to optimize your data storage, think about the magic of base62 conversion and how it enables efficient handling of large numbers with just sixty-two characters! 🚀

**Try it now challenge**:
- Write a second function the reverses the base62 digits into their original decimal.