<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ encoding - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ encoding - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 21 May 2026 16:11:36 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/encoding/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is base64 Encoding and Why is it Necessary? ]]>
                </title>
                <description>
                    <![CDATA[ By Sergei Bachinin In this article, we'll thoroughly explore base64 encoding. You'll learn how it came into being and why it's still so prevalent in modern systems. Here's what we'll cover: What is base64? Why use base64? When to use base64 A Case o... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-base64-encoding/</link>
                <guid isPermaLink="false">66d460ee706b9fb1c166b9a5</guid>
                
                    <category>
                        <![CDATA[ Base64 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ encoding ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 28 Nov 2023 18:30:41 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/11/cover-base65.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Sergei Bachinin</p>
<p>In this article, we'll thoroughly explore base64 encoding. You'll learn how it came into being and why it's still so prevalent in modern systems.</p>
<p>Here's what we'll cover:</p>
<ol>
<li><a class="post-section-overview" href="#heading-what-is-base64">What is base64?</a></li>
<li><a class="post-section-overview" href="#heading-why-use-base64">Why use base64?</a></li>
<li><a class="post-section-overview" href="#heading-when-to-use-base64">When to use base64</a></li>
<li><a class="post-section-overview" href="#heading-a-case-of-smtp">A Case of SMTP</a></li>
<li><a class="post-section-overview" href="#heading-are-these-limitations-still-relevant-today">Are These Limitations Still Relevant Today?</a></li>
<li><a class="post-section-overview" href="#heading-how-base64-helps-with-these-limitations">How base64 Helps with These Limitations</a></li>
<li><a class="post-section-overview" href="#heading-why-only-64-characters">Why Only 64 Characters?</a></li>
<li><a class="post-section-overview" href="#heading-do-i-have-to-use-base64-with-http11">Do I Have to Use base64 with HTTP/1.1?</a></li>
<li><a class="post-section-overview" href="#heading-why-is-base64-used-for-data-urls-is-there-a-more-efficient-encoding-for-this">Why is base64 Used for Data URLs?</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ol>
<h2 id="heading-what-is-base64">What is base64?</h2>
<p>Base64 is a way of transforming any data into a gibberish of digits and letters. Today it's widely used and taken for granted. Most of the time you use it because there is no choice: in many situations, only such text-like gibberish is considered <strong>valid</strong>.</p>
<p>At the same time, base64 is a costly instrument. It makes data about 33% larger in terms of memory usage. So base64 is one of these little things that make software <strong>slow</strong>. That's why you should use it only when it's absolutely necessary. </p>
<p>Again, typically you don't have a choice. But sometimes you do, and that's why it can be useful to understand how it works and what problems it is meant to solve.</p>
<p>This article is aimed at those who've already encountered base64 in their programming practice and may be wondering what on earth it is. You'll likely get more out of this guide if you have at least basic knowledge of data types and some understanding of how computer memory is organized at a binary level (bits, bytes and all that).</p>
<p>The encoding algorithm of base64 is straightforward. It takes as an input one binary sequence and ouputs another binary sequence. It doesn't care what the original bytes stood for – be it an image or a PDF or whatever. It just goes through the original binary stuff, splits it into chunks of 6 bits, and converts every chunk into a safe textual character (or, strictly speaking, a binary sequence that represents such a character). A "safe" character refers to one from a very limited set, which we'll discuss more later.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/base64-diagram-smaller-1.png" alt="That's approximately how the text &quot;Sun&quot; is encoded to base64" width="600" height="400" loading="lazy">
<em>This image shows approximately how the text "Sun" is encoded to base64</em></p>
<p>Then at some point you have to <strong>decode</strong> this pseudo-text back into an original data – because base64 gibberish in itself is useless. Decoding performs similar binary operations but in reverse. The restored data is guaranteed to be exactly the same as before encoding.</p>
<p>I won't discuss the encoding/decoding algorithm in more depth here. Wikipedia explains it in <a target="_blank" href="https://en.wikipedia.org/wiki/Base64">exhaustive detail</a> if you want to read more.</p>
<p>In practice, you (as a developer) will almost never deal with anything in binary itself when encoding your data. Instead, you'll use convenient wrappers that are available in almost any language, such as the <code>btoa()</code> and <code>atob()</code> functions in JavaScript. You can read more about them <a target="_blank" href="https://www.freecodecamp.org/news/encode-decode-html-base64-using-javascript/">here</a>.</p>
<h2 id="heading-why-use-base64">Why Use base64?</h2>
<p>What problems is base64 meant to solve? We have 3 main theories here:</p>
<ul>
<li>Base64 is used because some systems are <strong>restricted to ASCII</strong> characters but are actually used for all kinds of data. Base64 can "camouflage" this data as ASCII and thus help this data pass validation.</li>
<li>Because some older systems think that data consists of <strong>7-bit</strong> chunks (bytes), whereas modern ones use 8-bit bytes. This may lead to misunderstandings between systems. And base64 presumably can help with this – but it's not so obvious how.</li>
<li>Because some characters may have <strong>special meaning</strong> and this will differ from system to system. Base64 tackles this by using only the most "purely textual" characters from the ASCII set.</li>
</ul>
<p>Many experts on base64 advocate one theory and turn down all other theories. It may seem we should be able to (or that we need to) pick the right one from the list – but actually they are <em>all correct</em>. Well, the "7-bit problem" is much less of a problem today but the other two are still relevant and base64 addresses them both.</p>
<h2 id="heading-when-to-use-base64">When to Use base64</h2>
<p>When do you need to present data as base64 gibberish? In a great many situations, of course. But let's understand the main principle. </p>
<p>The official spec (RFC4648) says that base64 "is used to <strong>store</strong> or <strong>transfer</strong> data in environments that, perhaps for legacy reasons, are restricted to ASCII data".</p>
<p>So you need base64 when:</p>
<ul>
<li>Incompatible data is <strong>transmitted</strong> through the network. First of all it's a problem of emails – for example, encoding is necessary when you need to attach an image to a textual message. This was the reason why base64 was first introduced.</li>
<li>Incompatible data is <strong>stored</strong> in files or elsewhere. Often you need to embed non-textual data in a textual <strong>file</strong> like JSON, XML or HTML. Or to store something fancy in a brower <strong>cookie</strong> (and cookies must be only text).</li>
</ul>
<p>The official spec continues that base64 also:</p>
<ul>
<li>"makes it possible to manipulate objects with <strong>text editors</strong>". Apparently it means "rich text editors" such as Microsoft Word where you can combine text with images and other things. Do such programs use base64 for embedded content? That's possible, but let's leave it to more persistent researchers.</li>
</ul>
<h2 id="heading-a-case-of-smtp">A Case of SMTP</h2>
<p>SMTP is an age-old email protocol still prevalent today and used by Gmail, Outlook, and others. It defines how email messages must be transmitted between servers. Original SMTP rules were really strict, and by the end of 80s they were already a great nuisance.</p>
<p>First of all, SMTP allowed only plain text in the English language (which consists of only 128 characters known as ASCII). So some workarounds were necessary to send both non-English text and non-textual attachments.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/email-sending.png" alt="Email servers were able to communicate only using English text" width="600" height="400" loading="lazy">
<em>Diagram showing email going between two people via servers using English text only</em></p>
<p>Base64 solved this problem by converting all data to "safe English text". This conversion is necessary only for the time being, and when the danger is over, data is converted back to normal.</p>
<p>This is basically a very ugly practice that is somewhat similar to contraband. Data is obfuscated (well, really) in order to cheat a system that doesn't allow this kind of data.</p>
<p>And by the way, the restrictions of SMTP were meant for <strong>safety</strong> in the first place. But it was long ago and today nobody needs this safety anymore. Instead everyone needs an unobtrusive channel to transport anything whatsoever. So the old rules act like a pesky bureaucracy. But that's what legacy systems are and you have to live with them.</p>
<h2 id="heading-are-these-limitations-still-relevant-today">Are These Limitations Still Relevant Today?</h2>
<p>Yes, despite all the extensions and tricks.</p>
<p>Many restrictions of SMTP are relaxed thanks to various extensions. For instance, the "8BIT MIME" extension allows you to send email messages in 8-bit bytes and in characters other than ASCII. So in theory, today you can send a letter with an attachment without encoding.</p>
<p>But in practice it's still impossible to ignore the old restrictions. Because there are outdated email servers which didn't adopt new extensions. And you have to be able to communicate with them even if your own email server supports all the modern stuff.</p>
<p>Before sending a message to a certain email server, you first ask what kind of SMTP rules it supports. Does it implement an 8BIT MIME extension? If not, you will need to convert your message to the older format.</p>
<h2 id="heading-how-base64-helps-with-these-limitations">How base64 Helps with These Limitations</h2>
<p>It's self-evident that base64 must be a solution to an "ASCII only" problem because it transforms everything to ASCII characters. But it becomes less obvious when you combine it with the "<strong>7 bits</strong>" problem. Because no matter what kind of characters you use, they must be somehow transmittable by both 7-bit and 8-bit channels, depending on the situation.</p>
<p>Experts usually say something like:</p>
<blockquote>
<p>"Base64 transforms the binary sequence 01001101 01100001 01101110 <em>(whatever it means)</em> into text "TWFu".</p>
</blockquote>
<p>Such statements can lead you to think that something binary becomes non-binary. In fact, all ASCII characters produced by base64 are ultimately bits and bytes, just like the original data. (But of course the amount and order of bits change).</p>
<p>Here is a bash command to get a binary representation of a string:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span> -n <span class="hljs-string">"TWFu"</span> | xxd -b
</code></pre>
<p>It will tell you that "TWFu" is actually "<code>01010100 01010111 01000110 01110101</code>". But if every character is 8 bits long, a 7-bit channel may not recognize this TWFu as text. Apparently some additional binary juggling must take place to make it work for all channels.</p>
<p>Fortunately, with ASCII characters this binary juggling is easy. Because to store an ASCII character in memory you actually need only 7 bits. They can be fattened to 8 bits only if you need a conventional "octet". This is done simply by adding a "0" bit in front of the original seven. For example, "T" can be stored as both <code>1010100</code> and <code>01010100</code>.</p>
<p>So, conversion between 7-bit and 8-bit ASCII characters is a matter of adding/removing the leftmost "0" bit. Apparently email servers have to perform this kind of stuff when talking to each other.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/T-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>This image shows how a character "T" produced by base64 can be consumed by both 7- and 8-bit channels</em></p>
<p>So let's keep in mind that base64 in itself doesn't solve the "7 bit" problem. It just produces ASCII characters and this allows for a brisk conversion between 7 and 8 bits. But this conversion is the responsibility of a wider system, such as MIME (extension of SMTP).</p>
<h3 id="heading-memory-cost-depends-on-byte-length">Memory cost depends on byte length</h3>
<p>By the way, if you use only 7 bits per character, then base64 must be less wasteful in terms of memory usage.</p>
<p>The main theory is that base64 causes a memory overhead of 33% (or 37% or whatever). But it seems to be correct only for an 8-bit scenario.</p>
<p>Because, as I previously explained, base64 converts every 6-bit chunk of original data into a single ASCII character. If such a character is 8 bits long, it means that you are wasting 2 bits per every original chunk which is about 33%, just as promised by the experts. But with 7-bit characters this loss must be about half the size.</p>
<h2 id="heading-why-only-64-characters">Why Only 64 Characters?</h2>
<p>Base64 would be overkill if it was made only to bypass the ASCII restriction.</p>
<p>ASCII is 128 characters long, and if you could use all of them, the encoding algorithm would be more memory-efficient. It's cumbersome to explain but, in short, with a 128-long alphabet, a single character could represent 7 (instead of only 6) bits of original data.</p>
<p>But the authors of base64 decided to use only <em>half</em> of the ASCII characters, and surely they had a very good reason for that.</p>
<p>Characters can be wrong (unsafe) for at least two reasons:</p>
<ol>
<li>They can be <strong>invalid</strong> (forbidden by a system, like SMTP forbids anything beyond ASCII)</li>
<li>They can be valid <em>but</em> mistakenly recognized as a <strong>special character</strong></li>
</ol>
<p>In ASCII there are 30+ "control characters". They are <strong>not printable</strong> and are meant to cause some other effects. For example: "line feed", "backspace", "delete", "escape". Many of these commands are a legacy of some prehistoric devices like teleprinters.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/ascii-control-chars.png" alt="Non-printable ASCII characters" width="600" height="400" loading="lazy">
<em>Table showing 34 non-printable characters from ASCII set</em></p>
<p>Apparently you have to exclude all non-printable characters from the encoding alphabet. So you are left with some 90+ printable ones. But printable doesn't mean safe and reliable. They can also have <strong>special meaning</strong> in different systems. And a bunch of them have special meaning in SMTP, for example "@", "&lt;", and "&gt;". They will surely produce "some other effects" and it won't make you happy.</p>
<p>So it ended up with 64 chars – first of all, because 64 is easier to deal with algorithmically. And it looks like a really safe alphabet that can be used in a wide range of systems, not only SMTP.</p>
<p>Unfortunately it's not completely safe. There are <strong>only 62</strong> characters that are guaranteed to have no special meaning in all systems. They are digits, English small letters, and English capital letters. You need <strong>2 more</strong> characters for base64. And their choice differs from system to system.</p>
<p>The most popular candidates are "+" and "/" but still in some situations they will break stuff. For example, they have special meaning in URLs. That's why we have a "base64url" variant where the last two characters are "_" and "-".</p>
<h3 id="heading-base64-does-not-fix-the-problem-of-ambiguous-special-characters">Base64 does not fix the problem of "ambiguous" special characters</h3>
<p>Here I'd like to briefly explain one misconception that often pops up in various discussions about base64.</p>
<p>Some special characters are treated differently by different systems. The most notorious of them are "line feed" and "carriage return". Both are concerned with line breaks. But different systems have different opinions on how to combine these two chars to produce an actual line break.</p>
<p>There is a widespread belief that base64 somehow helps to reconcile these differences.</p>
<p>But base64 has nothing to do with how data is <strong>interpreted</strong> – for example, how text is displayed on a screen. Because in order to display something you first need to <strong>decode</strong> it from base64 back to the original form. Decoded data will be exactly the same as it was before encoding. It will be the same sequence of bits with the same ambiguous characters as before.</p>
<p>So, again, base64 can only <strong>conceal</strong> special chars for the time being. It doesn't magically sanitize data from dangerous chars.</p>
<h2 id="heading-do-i-have-to-use-base64-with-http11">Do I Have to Use base64 with HTTP/1.1?</h2>
<p>HTTP/1.1 is a text-based protocol. So you may ask whether non-textual data has to be encoded to transmit stuff over the Internet.</p>
<p>(Also, let's call it just HTTP. Keep in mind that HTTP/2 and /3 are binary protocols, so they are not in question).</p>
<p>HTTP actually allows <strong>all</strong> kinds of data in the <strong>body</strong> of a message. The body is not restricted to textual characters. So, for instance, an image can be sent in its original form, without jumbling its bits and bytes.</p>
<p>What is really "text-based" about HTTP is <strong>headers</strong>. Basically they are restricted to ASCII (this is not exactly true, but it's a good practice to use only ASCII).</p>
<p>Today HTTP headers are used in many different ways – and sometimes they have to carry non-ASCII stuff too. </p>
<p>For example, a basic HTTP authentication scheme suggests that you send username and password as part of the "Authentication" header. Username and password can contain a lot of incompatible stuff, so you have to encode them to safe textual chars. Base64 is recommended to use in such cases. For this reason some developers think that base64 is a kind of encryption (data protection) which is not true.</p>
<h2 id="heading-why-is-base64-used-for-data-urls-is-there-a-more-efficient-encoding-for-this">Why is base64 Used for Data URLs? Is There a More Efficient Encoding for This?</h2>
<p>Data URLs are probably the most well-known use of base64 today. It's a way to inline various assets like images (not links to them but their actual code) into HTML or CSS files.</p>
<p>Note that Data URLs have nothing to do with the transmission of data. Base64 is used here not because HTTP protocol forbids any binary sequences. When you send HTML or CSS file over network, HTTP doesn't care what's inside these files. But HTML and CSS files have to be only text to be properly interpreted (displayed) by text editors and browsers.</p>
<p>It makes sense, but still it's a pity – because, again, base64 is expensive. This notorious 33% or 37% of memory overhead is especially annoying with Data URLs. In most cases it defeats their purpose entirely. </p>
<p>The purpose is to improve <strong>performance</strong> of course. You get an image without an extra HTTP request and thus save some milliseconds. But this performance gain is small and easily nullified by the extra bytes created by base64.</p>
<p>So why base64 is used for data URLs at all? Could we use some less wasteful encoding for that (that is, encoding that uses a wider alphabet and thus outputs shorter strings of characters)?</p>
<p>At present it's very unlikely, because browsers allow only <strong>URL-safe characters</strong> in Data URLs. And there aren't too many URL-safe characters – just a tiny bit more than 64. Why are we restricted to URL-safe characters? Because we insert the encoded data into places where browsers expect a URL.</p>
<p>In theory, browsers could be smarter and relax this limitation when necessary. So let's ask a broader question next.</p>
<h3 id="heading-is-there-a-better-encoding-for-non-textual-data-in-html">Is there a better encoding for non-textual data in HTML?</h3>
<p>Technically, Data URLs are <strong>not the only way</strong> to embed non-textual data in HTML files. </p>
<p>You can build your homemade solutions where non-textual data is inserted elsewhere in HTML markup (attributes, innerText, or whatever). In such cases, you are not restricted to a URL-safe alphabet. So in theory, you can use any characters that are allowed in HTML.</p>
<p>Most HTML files use the UTF-8 character set. It contains <strong>all Unicode</strong> characters, and it's more than 1 million. So it must be possible to create some baseNNN encoding where at least 256 Unicode characters will be used. Such encoding would have no (or almost no) memory overhead.</p>
<p>In practice, everything is much more complicated.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/11/chars-3.png" alt="Image" width="600" height="400" loading="lazy">
<em>Diagram showing how few characters from UTF-8 are "really safe" and usable for a hypothetical baseNNN encoding</em></p>
<p>Why not encode stuff with Chinese characters, for example? Because they are too heavy. They take 3 or even 4 bytes of memory. That's how UTF-8 encoding works – it uses different number of bytes for different characters. And we are interested only in those that take <strong>1 byte</strong>.</p>
<p>(You may consider using multi-byte characters for our baseNNN encoding, but let's not go into that. You need only 1-byte ones – let's take it as an axiom.)</p>
<p>How many 1-byte characters are there in UTF-8? Only 128, and it's a <strong>good old ASCII</strong> range. Can you take all of them? No, because, again, you need only printable characters. You really need to see them in text editors and dev tools and elsewhere. </p>
<p>Then, just like in case of SMTP, you have to exclude a bunch of visible characters because they have special meaning in HTML. For example, double quotes (") won't do because it can prematurely terminate the Data URL string. This won't work:</p>
<p><code>&lt;img src="data:image/png;base64,iVBOR"w0KAA" /&gt;</code></p>
<p>So a possible alphabet again shrinks to 80-90 characters. This, in theory, allows you to create another encoding that will use a bit less memory than base64. </p>
<p>Such encodings actually exist, for example <strong>base85</strong> made in Adobe. It is more memory-efficient because it encodes 4 bytes of original data into 5 characters. But base85 is also much slower to compute, so its overall benefits are tiny, if any. And by the way it's not intended for web development and contains characters that can break things in HTML and CSS. (Though it must be possible to build a similar but web-friendly algorithm by swapping some characters.)</p>
<p>Can we find a better baseNNN for other kinds of text files (JSON, XML, and so on)? It seems unlikely, because these formats are predominantly UTF-8-encoded and this means roughly the same limitations as in case of HTML. Only the amount of special printable characters may differ (it's very small in JSON for example) but it's not a big deal.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Base64 was first introduced as a way to bypass a number of archaic restrictions imposed on email messages by the SMTP protocol. </p>
<p>Base64 allowed us to camouflage any data as text in order to pass validation when transmitted between email servers. It also ensured that this pseudo-text contained only safe characters, that is 1) only printable ones and 2) only those that have no special meaning in SMTP and (hopefully) in most other systems.</p>
<p>The final alphabet happened to be really narrow, and it allowed us to use base64 practically everywhere (but in slightly different variants). It helps in many cases where you have to mix textual and non-textual data. </p>
<p>Modern systems also use base64 despite its significant memory cost. At first glance this practice looks strange, because modern systems don't have these age-old restrictions that SMTP had. But it turns out that in most cases you still have very few cheap and non-special characters, and potential alternatives to base64 offer very small benefits.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Encode and Decode HTML Base64 using JavaScript – JS Encoding Example ]]>
                </title>
                <description>
                    <![CDATA[ When building an application or writing a program, you may need to encode or decode with HTML Base64 in JavaScript. This is possible thanks to two Base64 helper functions that are part of the HTML specification and are supported by all modern browser... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/encode-decode-html-base64-using-javascript/</link>
                <guid isPermaLink="false">66d45f768812486a37369cef</guid>
                
                    <category>
                        <![CDATA[ encoding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joel Olawanle ]]>
                </dc:creator>
                <pubDate>Wed, 08 Feb 2023 19:20:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/cover-template-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When building an application or writing a program, you may need to encode or decode with HTML Base64 in JavaScript.</p>
<p>This is possible thanks to two Base64 helper functions that are part of the HTML specification and are supported by all modern browsers.</p>
<p>In this article, you will learn about Base64 and how it works to convert binary data, regular <a target="_blank" href="https://www.freecodecamp.org/news/what-is-a-string-in-javascript/">strings</a>, and lots more into ASCII text.</p>
<h2 id="heading-what-is-base64">What is Base64?</h2>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Base64">Base64</a> is a group of binary-to-text encoding schemes representing binary data in ASCII string format. It is commonly used to encode data that needs to be stored or transmitted in a way that cannot be directly represented as text.</p>
<p>Base64 encoding works by mapping binary data to 64 characters from the ASCII character set. The 64 characters used in Base64 encoding are: <code>A-Z</code>, <code>a-z</code>, <code>0-9</code>, <code>+</code>, and <code>/</code>.</p>
<p>The encoding process takes 3 bytes of binary data and maps it to 4 characters from the above set, such that a single character represents every 6 bits of binary data. The result is a string of ASCII characters that can be transmitted or stored as text.</p>
<p>Base64 decoding is the reverse process of encoding. It takes a Base64 encoded string and maps each character back to its 6-bit binary representation. The resulting binary data is a reconstruction of the original binary data encoded to Base64.</p>
<h2 id="heading-how-to-encode-and-decode-html-base64-using-javascript">How to Encode and Decode HTML Base64 using JavaScript</h2>
<p>To encode and decode in JavaScript, you will use the <code>btoa()</code> and <code>atob()</code> JavaScript functions that are available and supported by modern web browsers.</p>
<p>These JavaScript helper functions are named after old Unix commands for converting binary to ASCII (btoa) and ASCII to binary (atob).</p>
<p>You can encode a string to base64 in JavaScript using the <code>btoa()</code> function and decode a base64 string using <code>atob()</code> function. For example, if you have a string stored in a variable, as seen below, you can first encode it to Base64:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> myString = <span class="hljs-string">"Welcome to freeCodeCamp!"</span>;
<span class="hljs-keyword">let</span> encodedValue = btoa(myString);
<span class="hljs-built_in">console</span>.log(encodedValue); <span class="hljs-comment">// V2VsY29tZSB0byBmcmVlQ29kZUNhbXAh</span>
</code></pre>
<p>You can also decode the <code>encodedValue</code> back to its original form using the <code>atob()</code> function. This function takes the encoded value and decodes it from Base64:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> myString = <span class="hljs-string">"Welcome to freeCodeCamp!"</span>;
<span class="hljs-keyword">let</span> encodedValue = btoa(myString);
<span class="hljs-keyword">let</span> decodedValue = atob(encodedValue);
<span class="hljs-built_in">console</span>.log(decodedValue); <span class="hljs-comment">// Welcome to freeCodeCamp!</span>
</code></pre>
<p>You now know how to encode and decode Base64 in JavaScript.</p>
<h2 id="heading-more-javascript-encoding-examples">More JavaScript Encoding Examples</h2>
<p>You can also encode binary data to Base64 encoded ASCII text in JavaScript using the <code>btoa()</code> function:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> binaryData = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>([<span class="hljs-number">72</span>, <span class="hljs-number">101</span>, <span class="hljs-number">108</span>, <span class="hljs-number">108</span>, <span class="hljs-number">111</span>, <span class="hljs-number">32</span>, <span class="hljs-number">87</span>, <span class="hljs-number">111</span>, <span class="hljs-number">114</span>, <span class="hljs-number">108</span>, <span class="hljs-number">100</span>]);
<span class="hljs-keyword">let</span> stringValue = <span class="hljs-built_in">String</span>.fromCharCode.apply(<span class="hljs-literal">null</span>, binaryData);
<span class="hljs-built_in">console</span>.log(stringValue); <span class="hljs-comment">// "Hello World"</span>

<span class="hljs-keyword">let</span> encodedValue = btoa(stringValue);
<span class="hljs-built_in">console</span>.log(encodedValue); <span class="hljs-comment">// SGVsbG8gV29ybGQ=</span>
</code></pre>
<p>In the above, you first converted Unicode values to characters and then encoded the string.</p>
<p>You can also decode the Base64 encoded ASCII text to binary data in JavaScript using the <code>atob()</code> function:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> encodedValue = <span class="hljs-string">"SGVsbG8gV29ybGQ="</span>;
<span class="hljs-keyword">let</span> binaryData = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(atob(encodedValue).split(<span class="hljs-string">""</span>).map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">c</span>) </span>{
    <span class="hljs-keyword">return</span> c.charCodeAt(<span class="hljs-number">0</span>);
}));
<span class="hljs-built_in">console</span>.log(binaryData); <span class="hljs-comment">// Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]</span>
</code></pre>
<h2 id="heading-wrapping-up">Wrapping Up!</h2>
<p>In this article, you have learned what Base64 means, how it works, and when to encode and decode in JavaScript.</p>
<p>Base64 is not intended to be a secure encryption method, nor is it intended to be a compression method, because encoding a string to Base64 typically results in a 33% longer output.</p>
<p>Base64 encoding is commonly used in JavaScript for situations like:</p>
<ul>
<li><p>Storing and transmitting binary data as text.</p>
</li>
<li><p>Encrypting data where the encoded data is sent over an insecure channel and decoded on the other end. However, it should not be considered a secure encryption method, as it can easily be decoded.</p>
</li>
<li><p>Data transfer between systems with different character sets.</p>
</li>
<li><p>Storing binary data in a database.</p>
</li>
</ul>
<p>Thanks for reading and have fun coding!</p>
<p>You can access over 185 of my articles by <a target="_blank" href="https://joelolawanle.com/contents">visiting my website</a>. You can also use the search field to see if I've written a specific article.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Unicode Characters – What Every Developer Should Know About Encoding ]]>
                </title>
                <description>
                    <![CDATA[ If you are coding an international app that uses multiple languages, you'll need to know about encoding. Or even if you're just curious how words end up on your screen – yep, that's encoding, too. I'll explain a brief history of encoding in this arti... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/everything-you-need-to-know-about-encoding/</link>
                <guid isPermaLink="false">66bc55d2cd8a65d579e3aa06</guid>
                
                    <category>
                        <![CDATA[ ascii ]]>
                    </category>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ encoding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ unicode ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kealan Parr ]]>
                </dc:creator>
                <pubDate>Mon, 01 Mar 2021 16:01:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/12/Title.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you are coding an international app that uses multiple languages, you'll need to know about encoding. Or even if you're just curious how words end up on your screen – yep, that's encoding, too.</p>
<p>I'll explain a brief history of encoding in this article (and I'll discuss how little standardisation there was) and then I'll talk about what we use now. I'll also cover some <strong>Computer Science</strong> theory you need to understand.</p>
<h2 id="heading-introduction-to-encoding">Introduction to Encoding</h2>
<p>A computer only can understand binary. Binary is the language of computers, and is made up of <code>0</code>'s and <code>1</code>'s. There is nothing else allowed. One digit is called a <code>bit</code>, and a <code>byte</code> is 8 bits. So 8 <code>0</code>'s or <code>1</code>'s make up one <code>byte</code>.</p>
<p>Everything eventually ends up as binary – programming languages, mouse moves, typing, and all the words on the screen.</p>
<p>If all the text you're reading was once binary too, then how do we turn binary into text? Let's look at what we used to do back in the beginning.</p>
<h2 id="heading-a-brief-history-of-encoding">A Brief History of Encoding</h2>
<p>In the early days of the internet, it was English only. We didn't need to worry about any other characters and the <strong>American Standard Code for Information Interchange</strong> (<strong>ASCII</strong>) was the character encoding that fit this purpose. </p>
<p><strong>ASCII</strong> is a mapping, from binary to alphanumeric characters. So when the PC receives binary:</p>
<pre><code><span class="hljs-number">01001000</span> <span class="hljs-number">01100101</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01101111</span> <span class="hljs-number">00100000</span> <span class="hljs-number">01110111</span> <span class="hljs-number">01101111</span> <span class="hljs-number">01110010</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01100100</span>
</code></pre><p>With <strong>ASCII</strong> it can translate that into "Hello world".</p>
<p>One byte (eight bits) was large enough to fit every English character, and some control characters too. Some of these control characters were used for instruments called teleprinters, so at the time they were useful (not so much now!) </p>
<p>But the control characters were things like  7 (<code>111</code> in binary) that would make a bell sound on your PC, 8 (<code>1000</code> in binary) that would print over the last character it just printed, or 12 (<code>1100</code> in binary) that would clear a video terminal from all the text just written.</p>
<p>Computers at this time were using 8 bits for one byte (they didn't always), so there were no issues. We could store all our control characters, all our numbers, all the English characters and have some left! Because one byte can encode 255 characters, and ASCII only needed 127 characters. So we had 128 encodings that were unused.</p>
<p>Let's look at an ASCII table here to see every character. All lowercase and uppercase A-Z and 0-9 were encoded to binary numbers. Remember the first 32 are unprintable control characters.</p>
<h2 id="heading-ascii-character-table">ASCII Character Table</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-172.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Can you see how it ends with 127? We have some spare room at the end.</p>
<h1 id="heading-issues-with-ascii">Issues with ASCII</h1>
<p>The spare characters were 127 through to 255. People began to think about what would be best to fill those remaining characters. <strong>But everyone had different ideas about what those final characters should be.</strong></p>
<p>The American National Standards Institute (<strong>ANSI</strong> - don't get confused with <strong>ASCII</strong>) is a standards body for establishing standards across lots of different fields. They decided what everyone was doing with 0-127, which is what <strong>ASCII</strong> was already doing. But the rest were open.</p>
<p>No one was debating what 0-127 in the <strong>ASCII</strong> encoding was. The problem was with the <strong>spare ones</strong>.</p>
<p>Below is what the first IBM computers did with the 128-255 encodings for ASCII.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-169.png" alt="Image" width="600" height="400" loading="lazy">
<em>Some squiggles, some background icons, math operators and some accented characters like é.</em></p>
<p>But other computers didn't all follow this. And everyone wanted to implement their own encodings for the end of <strong>ASCII</strong>.</p>
<p>These different endings for <strong>ASCII</strong> were called <strong>code pages</strong>.</p>
<h3 id="heading-what-are-ascii-code-pages">What are ASCII Code Pages?</h3>
<p><a target="_blank" href="https://www.aivosto.com/articles/charsets-codepages.html">Here's</a> a collection of over 465 different codepages! You can see that there were multiple codepages <strong>EVEN</strong> <strong>for the same language</strong>. Greek and Chinese both have multiple codepages, for example.</p>
<p>So how on EARTH were we ever going to standardise this? Or make it work between differing languages? Between the same language with different codepages? In a non English language? </p>
<p>Chinese has over 100,000 different characters. We don't even have enough spare characters for Chinese, let alone agreeing that the final characters should be Chinese ones. This isn't looking so good.</p>
<p>This problem even has its own term: <strong>Mojibake</strong>.</p>
<p>It's garbled text you may sometimes see from decoding text, but using the wrong decoding. It means <strong>character transformation</strong> in Japanese.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-171.png" alt="Image" width="600" height="400" loading="lazy">
<em>Example of completely garbled text (mojibake).</em></p>
<h2 id="heading-this-sounds-a-little-insane">This sounds a little insane...</h2>
<p>Exactly! We will have zero chance of reliably interchanging data.</p>
<p>The internet is just a huge connection of computers around the world. Imagine if all these countries decided what they each thought the standards should be. If the Greek computers only accepted Greek and the English computers only sent English...? You would just be shouting into an empty cave. No one would understand you. And no one would be able to decode the nonsense.</p>
<p>ASCII wasn't fit for real life use. In a global, connected internet, we had to evolve, or else forever deal with hundreds of codepages.</p>
<p>��� <strong>Unless you</strong> ������ <strong>fancied trying</strong> ��� <strong>to</strong> ��� <strong>read paragraphs like this.</strong> �֎֏0590֐��׀ׁׂ׃ׅׄ׆ׇ</p>
<h2 id="heading-along-came-unicode">Along Came Unicode</h2>
<p>Unicode is sometimes called the <a target="_blank" href="https://en.wikipedia.org/wiki/Universal_Coded_Character_Set">Universal Coded Character Set</a> (UCS), or even ISO/IEC 10646. But Unicode is its more common name.</p>
<p>But, this is where Unicode entered the scene to help solve the problems that <strong>encoding</strong> and <strong>code pages</strong> were causing.</p>
<p>Unicode is made up of lots of <strong>code points</strong> (mapping lots of characters from around the world to a key that all computers can reference.) A collection of <strong>code points</strong> is called a <strong>character set</strong> - which is what Unicode is.</p>
<p>We can map something abstract to a letter we want to reference. And it does every character! Even <a target="_blank" href="https://unicode.org/charts/PDF/U13000.pdf">Egyptian Hieroglyphs</a>.</p>
<p>Some people did all the hard work of mapping what each character would be (in all the languages) to a key that we could all access. They look like this:</p>
<p><strong>"Hello World"</strong> </p>
<h6 id="heading-u0048-latin-capital-letter-h">U+0048 : LATIN CAPITAL LETTER H</h6>
<p>U+0065 : LATIN SMALL LETTER E<br>U+006C : LATIN SMALL LETTER L<br>U+006C : LATIN SMALL LETTER L<br>U+006F : LATIN SMALL LETTER O<br>U+0020 : SPACE [SP]<br>U+0057 : LATIN CAPITAL LETTER W<br>U+006F : LATIN SMALL LETTER O<br>U+0072 : LATIN SMALL LETTER R<br>U+006C : LATIN SMALL LETTER L<br>U+0064 : LATIN SMALL LETTER D</p>
<p>The U+ lets us know it's the Unicode standard, and the number is what results when the binary get's transformed to numbers. It uses <a target="_blank" href="https://www.bbc.co.uk/bitesize/guides/zp73wmn/revision/1#:~:text=Hexadecimal%20(or%20hex)%20is%20a,values%20in%20binary%20and%20denary.">hexadecimal</a> notation which is just a simpler way of representing binary numbers. You don't have to worry too much about the hexadecimal here, though. </p>
<p><a target="_blank" href="https://www.babelstone.co.uk/Unicode/whatisit.html">Here's</a> a link where you can type whatever you want into the text box, and see the Unicode character encoding. Or look at all 143,859 Unicode character points <a target="_blank" href="https://unicode-table.com/en/">here</a>. You can also see where each character is from in the world!</p>
<p>I just want to be clear. At this point we have a big dictionary of <strong>code points</strong> mapping to characters. A really big <strong>character set</strong>. Nothing more. </p>
<p><strong>There's one final ingredient we need to add to our mix.</strong></p>
<h2 id="heading-unicode-transform-protocol-utf">Unicode Transform Protocol (UTF)</h2>
<p>UTF is a way we encode Unicode code points. The UTF encodings are defined by the Unicode standard, and are able to encode every single Unicode <strong>code point</strong> we need.</p>
<p>But there are different types of UTF standards. They differ depending on the amount of bytes used to encode one <strong>code point</strong>. It also depends on whether you're using <strong>UTF-8</strong> (one byte per code point), <strong>UTF-16</strong> (two bytes per code point) or <strong>UTF-32</strong> (four bytes per code point).</p>
<p>If we have these different encodings, how do we know which encoding a file will use? There's a thing called a <strong>Byte Order Mark</strong> (<strong>BOM</strong>) - sometimes referred to as an <strong>Encoding Signature</strong>. The <strong>BOM</strong> is a two-byte marker at the beginning of a file that tells what encoding the file is using.</p>
<p><strong>UTF-8</strong> is the most used on the internet, and is also specified in HTML5 as the preferred encoding for new documents, so I'll spend the most time explaining this one.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-163.png" alt="Image" width="600" height="400" loading="lazy">
<em>You can see in the <a target="_blank" href="https://en.wikipedia.org/wiki/UTF-8#/media/File:Utf8webgrowth.svg">diagram </a>even from 2012, UTF-8 was widely becoming the most used encoding. And for the web it still is.</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-179.png" alt="Image" width="600" height="400" loading="lazy">
_W3 <a target="_blank" href="https://w3techs.com/technologies/cross/character_encoding/ranking">diagram </a>to show how well used UTF-8 is used on a variety of websites._</p>
<h2 id="heading-what-is-utf-8-and-how-does-it-work">What is UTF-8 and How Does it Work?</h2>
<p><strong>UTF-8</strong> encodes all the Unicode code points from 0-127 in 1 byte (the same as <strong>ASCII</strong>). This means that if you were coding your program using <strong>ASCII</strong>, and your users used <strong>UTF-8,</strong> they <em>wouldn't notice anything was wrong</em>. Everything would just work. </p>
<p>Just remember how strong a selling point this is. We needed to remain <strong>ASCII</strong> backwards compatible while <strong>UTF-8</strong> was being implemented and used by everyone. It doesn't break anything currently being used.</p>
<p>Because it's called <strong>UTF-8</strong>, remember that's the minimum number of bits (8 bits being one byte!) that a <strong>code point</strong> will be. There are other Unicode characters that are stored in multiple bytes (up to 6 bytes depending on the character). This is what people mean when the encoding is called <strong>variable length</strong>.</p>
<p>It might be more, depending on the language. English is 1 byte. <a target="_blank" href="https://design215.com/toolbox/ascii-utf8.php">European (Latin), Hebrew and Arabic</a> are represented with 2 bytes. 3 bytes are used for <a target="_blank" href="https://design215.com/toolbox/utf8-3byte-characters.php">Chinese, Japanese, Korean and other Asian characters</a><em>.</em> You get the idea. </p>
<p>When you need a character to span more than one byte, you have a bit combination to identify a continuation sign, saying this character is continued over the next several bytes. So you’ll still only use one byte per character for English, but if you need a document to contain some foreign characters, you can do that too.</p>
<p>And now, wonderfully, we can all agree on what the <a target="_blank" href="https://en.wikipedia.org/wiki/Cuneiform_(Unicode_block)">Sumerian cuneiform characters</a> encoding is (𒀵 𒁷𒂅 𒐤), as well as some <a target="_blank" href="https://unicode.org/emoji/charts/full-emoji-list.html">emoji's</a> 😉😉 so we can all communicate!</p>
<p>The high level overview is: You first read the <strong>BOM</strong> so you know your encoding. You decode the file into Unicode <strong>code points</strong>, and then represent the characters from the Unicode character set into characters drawn onto the screen.</p>
<h2 id="heading-a-final-word-about-utf">A Final Word About UTF</h2>
<p>Remember, encoding is <strong>key</strong>. If I send the complete wrong encoding you can't read anything. Be aware of it when receiving or sending data. Often it is abstracted away in the tools you use everyday, but as programmers it's important to understand what is happening under the hood. </p>
<p>How do we specify our encodings, then? Because HTML is written in English, and almost all encodings can deal with English fine. We can embed it right at the top in the <code>&lt;head&gt;</code> section.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<p>It's important to do this at the very start of the <code>&lt;head&gt;</code>, as the parsing of the <a target="_blank" href="https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding">HTML may have to start again</a> if the encoding it's currently using is wrong.</p>
<p>We also can get the encoding from the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type">Content-Type</a> header from the HTTP request/ response.</p>
<p>If an HTML document doesn't contain the encoding tag, the <a target="_blank" href="https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding">HTML5 spec</a> has some interesting ways it can guess the encoding called <a target="_blank" href="https://encoding.spec.whatwg.org/#bom-sniff"><strong>BOM sniffing</strong></a>. This is where it guesses the encoding from the <strong>Byte Order Mark</strong> (<strong>BOM</strong>) we discussed earlier. </p>
<h2 id="heading-so-is-that-it">So is that it?</h2>
<p>Unicode isn't finished. Like any standard, we add, remove and make new proposals to the standard. No specification is ever considered "complete".</p>
<p>There are generally 1 or 2 release a year, and you can find them <a target="_blank" href="https://unicode.org/history/publicationdates.html">here</a>.</p>
<p>Recently I read about a very interesting bug around <a target="_blank" href="https://twitter.com/availablegreen/status/1332774350613835779">Twitter rendering Russian Unicode characters incorrectly</a>. </p>
<p>If you have read this far, congratulations – it's a lot to digest.</p>
<p>I would encourage you to do one last piece of homework. </p>
<p>Look at how broken websites can really be when the encoding is wrong. I used <a target="_blank" href="https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfilgdlmhefphglae?hl=en">this</a> Google Chrome extension and changed my encoding and tried to read webpages. The message was completely unclear. Try and read this article. Try and navigate Wikipedia. See <strong>Mojibake</strong> for yourself.</p>
<p>It helps to see how important encoding truly is.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-164.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In my time spent researching and trying to simplify this article, I learned about <a target="_blank" href="https://en.wikipedia.org/wiki/Michael_Everson#">Michael Everson</a>. Since 1993, he has proposed over 200 Unicode changes, and has added thousands of characters to the standard. As of 2003, he was credited as the leading contributor of Unicode proposals. He is one huge reason why Unicode is what it is. Very impressive, and he has done a great deal for the Internet as we know it.</p>
<p>I hope this has explained a good overview of why we need encodings, what problems encoding solves, and what happens when it goes wrong.</p>
<p>I share my writing on <a target="_blank" href="https://twitter.com/kealanparr">Twitter</a> if you enjoyed this article and want to see more.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
