Understanding Different Types of Encoding and Decoding in Programming with Practical Examples

Pratiyush
4 min readMar 10, 2024

--

Encoding and decoding are fundamental processes in programming that involve converting data from one form to another. These processes are critical for data storage, transmission, and communication between different systems or components. Various encoding schemes exist, each designed for specific types of data and use cases. Below, we discuss some common types of encoding and provide practical examples.

Types of Encoding

1. ASCII Encoding

ASCII (American Standard Code for Information Interchange) is a character encoding standard for electronic communication. It represents text in computers, telecommunications equipment, and other devices that use text.

2. Unicode Encoding

Unicode is a computing industry standard designed to consistently encode, represent, and handle text expressed in most of the world’s writing systems. UTF-8, UTF-16, and UTF-32 are common Unicode encoding schemes, each using different byte sizes to represent a character.

3. Base64 Encoding

Base64 is used to encode binary data into an ASCII string format by converting it into a radix-64 representation. It is commonly used in web applications to encode binary data like images or files for transmission over media that are designed to deal with textual data.

4. URL Encoding

URL encoding, also known as percent encoding, is used to encode special characters in URLs by replacing them with one or more character triplets that consist of the percent character “%” followed by two hexadecimal digits.

5. Binary Encoding

Binary encoding involves converting data into a binary format, which is a base-2 numeral system representation using only two different symbols: typically 0 (zero) and 1 (one).

Learn it by Examples

1. ASCII Encoding Example

Converting the string “Hello” into ASCII codes:

 H -> 72
e -> 101
l -> 108
l -> 108
o -> 111

Each character is represented by a numerical code. ASCII encoding is extensively used for creating and editing text files, including HTML, JSON, and source code for programming languages. It ensures that basic English text is represented in a uniform manner across different systems.

Practical Example 1: Source Code Files

Programming and scripting language files (.c, .py, .js, etc.) typically use ASCII encoding for their text. This ensures that the source code can be universally read and written by developers across different platforms. For instance, a Python script with a print statement, print("Hello, world!"), uses ASCII encoding for each character, making the script easily shareable and executable on any system supporting Python.

2. Unicode Encoding Example (UTF-8)

Encoding the emoji “😊” in UTF-8:

 😊 -> F0 9F 98 8A 

This shows how UTF-8 encodes characters that are beyond the ASCII range.

Practical Example 1: Multilingual Websites and Applications

Unicode encoding, especially UTF-8, is crucial for developing websites and applications that support multiple languages, including those with complex characters like Chinese, Arabic, and emoji. It ensures that text appears correctly regardless of the user’s system language settings.

Practical Example 2: Database Storage

Databases that store text data from multiple languages use Unicode encoding to maintain data integrity and support global accessibility.

3. Base64 Encoding Example

Encoding the binary data of an image into a Base64 string for embedding in an HTML document:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA…">

This allows the image to be displayed directly without needing a separate file.

Practical Example 1: Email Attachments

Base64 encoding is used to convert binary files like images or PDFs into ASCII text, which can then be sent as email attachments. This ensures that binary data can safely traverse email systems that were traditionally designed to handle text-only data.

Practical Example 2: Embedding Images in HTML/CSS

Developers use Base64 encoding to embed images directly into HTML or CSS files as a data URI, reducing HTTP requests for small images and speeding up page loads.

4. URL Encoding Example

Encoding a URL that includes spaces and special characters:

Original URL: https://example.com/query?name=John Doe & age=30
Encoded URL: https://example.com/query?name=John%20Doe%20&%20age=30

Spaces are encoded as `%20`, ensuring the URL is valid for web transmission.

Practical Example 1: Web Form Submission

When a user submits a form on a website, the form data is URL encoded to safely transmit special characters (like spaces and punctuation) over the internet. This ensures that the data received by the server is accurate and not corrupted by the transmission process.

Practical Example 2: Dynamic URLs Creation

Encoding is used to construct dynamic URLs that include variable data, such as search queries. It ensures that the URLs are valid and can be properly processed by web servers.

5. Binary Encoding Example

Converting the number 15 into binary format:

15 -> 1111

This binary encoding represents the decimal number 15 in a format that can be directly processed by digital systems.

Practical Example 1: Computer Networking

Binary encoding is foundational to data transmission in computer networks, where data, regardless of its original format (text, images, video), is converted into binary and transmitted over network protocols like TCP/IP.

Practical Example 2: File Storage

All types of files stored on a computer, from documents to executables, are ultimately stored as binary data. Binary encoding enables the storage and retrieval of diverse data types on digital storage media.

Each type of encoding serves specific purposes, from representing text in a computer-readable format to encoding data for safe transmission over the internet. Understanding these encoding schemes and their appropriate use cases is crucial for developers and engineers working with data transmission, storage, and representation across various applications and systems.

--

--