Atob() and Unit8Array in simple words

Photo by Markus Spiske on Pexels.com

What is Atob()

atob is a JavaScript function that stands for “ASCII to binary”. It’s used to convert a base64-encoded string into its original binary data.

Base64 is a way of encoding binary data (such as images, audio, or PDFs) into ASCII characters, so that it can be easily transported over a network or stored as text. This is useful because some systems can’t handle binary data directly, and text can be stored and transmitted much more easily. When the data needs to be used again, the reverse process of converting the base64 text back into binary data is performed.

The atob function takes a base64-encoded string as input, and returns the original binary data as a string of ASCII characters. For example, you might have a base64-encoded string that represents a PDF file, and you can use atob to convert that string back into a binary representation of the PDF file.

In a sense, you can think of atob as a “decoder” that converts a base64 string into its original form. The reverse function, btoa, is used to convert binary data into a base64 string.

What Unit8Array is

A Uint8Array is a data structure in JavaScript that is used to store and work with binary data. It’s a special kind of array that can store numbers between 0 and 255, which is perfect for representing binary data like the pixels in an image, the samples in a sound clip, or the bytes in a PDF file.

Think of a Uint8Array like a row of lockers in a school. Each locker can store a single item, just like each element in the array can store a single number. But the lockers in this row can only store numbers between 0 and 255, just like the elements in the Uint8Array can only store numbers between 0 and 255.

When you work with binary data, you often want to convert it into a Uint8Array so that you can more easily manipulate the individual bytes. For example, you might want to change a certain byte to make the image look different, or you might want to extract certain bytes to get information about the file. The Uint8Array makes it easy to do these kinds of operations because it provides a convenient way to access and manipulate the individual bytes of binary data.

How the atob() and Unit8Array work together

atob and Uint8Array work together to convert a base64-encoded string into its original binary data. Here’s how it works in simple terms:

  1. The base64-encoded string is passed as an argument to the atob function.
  2. atob decodes the base64-encoded string into its original binary data, which is represented as a string of ASCII characters.
  3. The binary data is then converted into a Uint8Array, which is a special kind of array that can store individual bytes of binary data.
  4. The Uint8Array can now be used to manipulate the binary data in various ways, for example by writing it to a file, displaying it in a browser, or processing it in some other way.

So, atob is used to convert the base64-encoded string into its original binary data, and Uint8Array is used to store that data in a format that makes it easier to work with. Together, they provide a way to efficiently and conveniently process binary data in JavaScript.