<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: toolstead</title>
    <description>The latest articles on DEV Community by toolstead (@toolstead).</description>
    <link>https://dev.to/toolstead</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4205621%2Fee953e6b-6139-45ea-9c9c-fa8667a4e5c2.png</url>
      <title>DEV Community: toolstead</title>
      <link>https://dev.to/toolstead</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toolstead"/>
    <language>en</language>
    <item>
      <title>Bit Length Is Not a Format: Five 34-Bit Layouts</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Mon, 23 Nov 2026 01:05:31 +0000</pubDate>
      <link>https://dev.to/toolstead/bit-length-is-not-a-format-five-34-bit-layouts-5gg8</link>
      <guid>https://dev.to/toolstead/bit-length-is-not-a-format-five-34-bit-layouts-5gg8</guid>
      <description>&lt;p&gt;Ask someone in the trade for "the 34-bit Wiegand format" and you will usually get one answer: 16 bits of facility code, 16 bits of card number, two parity bits. That answer is right for the credential in their hand and wrong for the one on the next door.&lt;/p&gt;

&lt;p&gt;Thirty-four bits is a length. A length constrains how many bits arrive; it says nothing about where the fields sit. In the published tables there are five layouts sharing that length, from four vendors, and two of them overlap in a way that produces a facility code nobody will question until a cardholder is standing at a door that will not open.&lt;/p&gt;

&lt;p&gt;This is not a curiosity. It is the single most common way a working decoder returns a confidently wrong answer.&lt;/p&gt;

&lt;h1&gt;
  
  
  The five
&lt;/h1&gt;

&lt;p&gt;The most widespread is HID H10306, also documented by Honeywell as N10002. A leading parity bit, sixteen bits of facility code, sixteen bits of card number, a trailing parity bit. With 65,536 facility codes and 65,536 cards per facility, it is the natural step up from &lt;a href="https://toolstead.net/blog/wiegand-26-vs-34/" rel="noopener noreferrer"&gt;the 26-bit standard&lt;/a&gt; when a site runs out of facility codes.&lt;/p&gt;

&lt;p&gt;AWID's RS2 has the same total length and a different split: eight bits of site code, twenty-four bits of card number. Read an RS2 credential with an H10306 decoder and you get a facility code built from the top eight bits of the site field plus the first eight bits of the real card number. Both numbers look plausible. Neither is the number the panel was programmed with. The frame passes parity, because parity is about bits, not meaning. The controller accepts it, the door does not open, and the installer spends an afternoon on the reader instead of the credential.&lt;/p&gt;

&lt;p&gt;Indala's Optus format splits eleven bits of site against sixteen bits of card, and the published table places them apart: site code in the high bits, card number near the front. Seven of the thirty-four columns are marked for neither field. That gap is information, not an omission to be tidied away.&lt;/p&gt;

&lt;p&gt;Cardkey Smartpass carries three fields, not two. Thirteen bits of site, three bits of issue level, sixteen bits of card number. The issue level is the one that gets dropped. Drop it and you are decoding a card number that is off by whatever the issue level contributed — usually zero, sometimes not, which is worse than always wrong, because it works often enough to look correct.&lt;/p&gt;

&lt;p&gt;BQT takes a fourth approach: eight bits of facility code and twenty-four bits of card number. Same shape as AWID RS2, different vendor, and unlike RS2 this one publishes its parity rows — over spans that do not line up with the field boundary. The even region runs across the end of the facility code and the first eight bits of the card number. So parity and field layout are two separate questions, and getting one right tells you nothing about the other.&lt;/p&gt;

&lt;h1&gt;
  
  
  The seven empty columns
&lt;/h1&gt;

&lt;p&gt;The Indala entry is worth pausing on, because the instinct when you see a table with gaps is to fill them. Thirty-four bits minus twenty-seven assigned leaves seven. Two of those are plausibly parity. The other five are unknown.&lt;/p&gt;

&lt;p&gt;You can always construct a tidy reading. Put the parity at the ends, close the gap, and the table looks professional. What you have actually done is invent a layout, because the source you are copying from did not know either. If a frame later fails to decode, you will not suspect the seven bits, because nothing in your tool says they were guessed.&lt;/p&gt;

&lt;p&gt;Leaving them visible — "eleven site, sixteen card, seven unaccounted" — means a user whose frames do not decode has somewhere to look. That is the difference between a tool that is sometimes wrong and a tool that tells you when it does not know.&lt;/p&gt;

&lt;h1&gt;
  
  
  Even the good references have typos
&lt;/h1&gt;

&lt;p&gt;While checking our own table against a widely used public format reference, we found an entry labelled HID 34-bit (N1002) with eight bits of site code and sixteen bits of card number. Twenty-four data bits. A 34-bit frame carries thirty-two.&lt;/p&gt;

&lt;p&gt;The arithmetic settles it without needing another source: a layout that adds up to twenty-six bits cannot be a thirty-four-bit format. Three other references — including the Honeywell format specification that gives a per-bit map — put N10002 at sixteen plus sixteen. The entry is a typo in an otherwise excellent table.&lt;/p&gt;

&lt;p&gt;The lesson is not that the table is bad. It is that a field layout has to be checked by arithmetic before it is trusted. Sum the fields, add the parity bits, compare to the length. A table with four columns of plausible numbers will not be caught by reading it.&lt;/p&gt;

&lt;h1&gt;
  
  
  What to do with a frame in hand
&lt;/h1&gt;

&lt;p&gt;The practical rule: never let a bit count choose a layout. Two questions settle almost every case.&lt;/p&gt;

&lt;p&gt;First, what is the facility code? An eight-bit site field tops out at 255. A facility code of 12,000 cannot have come from an eight-bit field, which rules out AWID RS2 and points at H10306. This one check resolves most mislabelled frames.&lt;/p&gt;

&lt;p&gt;Second, how many fields does the credential actually have? If the card is enrolled with a site, an issue level and a card number, Cardkey Smartpass is in play and the issue level must be preserved through every conversion.&lt;/p&gt;

&lt;p&gt;Past that, the honest answer is that a 34-bit frame carries no self-describing marker of its layout. The format number is printed on the credential or stored in the panel. Anything else is inference.&lt;/p&gt;

&lt;p&gt;Which is why a decoder that makes you pick the layout, instead of picking for you, is not being pedantic. Ours asks before it answers, and refuses the widths where the answer would be a guess — see &lt;a href="https://toolstead.net/wiegand-26-34-converter/" rel="noopener noreferrer"&gt;the frame decoder&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why this keeps happening
&lt;/h1&gt;

&lt;p&gt;Parity protects bits, not meaning. A decoder that checks parity confirms the frame arrived intact; it says nothing about whether the field boundaries are right. Add that a wrong split still produces numbers inside the valid ranges of the fields it thinks it is reading, and you have a failure mode with no alarm attached.&lt;/p&gt;

&lt;p&gt;The 37-bit length has the same problem, worse: H10302 carries no facility code at all, and H10304 carries sixteen bits of one. Same vendor, same length, incompatible. Thirty-six bits has at least four layouts. Thirty-two has five, two of which have no parity bits, so a corrupted frame cannot even be detected.&lt;/p&gt;

&lt;p&gt;The pattern is consistent enough to state plainly: in this protocol, the length is the least informative part of the frame.&lt;/p&gt;

&lt;h1&gt;
  
  
  Checking our own work
&lt;/h1&gt;

&lt;p&gt;We keep the layouts we could source with a per-bit reference in one table, with a confidence level on every row and an explicit count of bits that are not assigned to any field. Where sources disagree — the 37-bit parity boundary is the clearest case — the row says so rather than picking a winner.&lt;/p&gt;

&lt;p&gt;The table is at &lt;a href="https://toolstead.net/wiegand-bit-formats/" rel="noopener noreferrer"&gt;/wiegand-bit-formats/&lt;/a&gt;, and the dataset behind it is published separately so it can be corrected rather than merely read. If a row is wrong for the systems you work with, the fix is worth more to us than the traffic.&lt;/p&gt;

</description>
      <category>hardware</category>
      <category>security</category>
      <category>programming</category>
      <category>embedded</category>
    </item>
    <item>
      <title>What Is a Base64 Data URI — and Why Some Images Won't Decode</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Fri, 20 Nov 2026 01:34:31 +0000</pubDate>
      <link>https://dev.to/toolstead/what-is-a-base64-data-uri-and-why-some-images-wont-decode-1fb2</link>
      <guid>https://dev.to/toolstead/what-is-a-base64-data-uri-and-why-some-images-wont-decode-1fb2</guid>
      <description>&lt;p&gt;A data URI is an image that lives inside a string instead of a file. The whole thing — header, encoding flag and payload — is one line of text you can paste into a CSS rule, a JSON field, or an address bar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That single line is a 1×1 transparent PNG. Every part of it is load-bearing, and almost every "this Base64 won't decode" report traces back to one of three places: the header is malformed, the payload is damaged, or the bytes were never an image to begin with. Those are three different bugs with three different fixes, and telling them apart takes about ten seconds once you know what to look at.&lt;/p&gt;

&lt;h1&gt;
  
  
  The grammar, which is shorter than people expect
&lt;/h1&gt;

&lt;p&gt;The format is defined by RFC 2397 and the whole thing is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data:[&amp;lt;mediatype&amp;gt;][;base64],&amp;lt;data&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the comma is mandatory. If you omit everything before it, &lt;code&gt;data:,Hello&lt;/code&gt; is a valid data URI whose media type defaults to &lt;code&gt;text/plain;charset=US-ASCII&lt;/code&gt;. The &lt;code&gt;;base64&lt;/code&gt; token is a flag, not a MIME parameter — it tells the parser "the payload is base64-encoded", and without it the payload is interpreted as percent-encoded text.&lt;/p&gt;

&lt;p&gt;That distinction explains a failure people hit constantly. Take a valid image payload but drop the &lt;code&gt;;base64&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data:image/png,iVBORw0KGgoAAAANSUhEUg...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser now reads &lt;code&gt;iVBORw0KGgo...&lt;/code&gt; as literal text and shows nothing. No error, no warning — the image simply doesn't appear, because as far as the parser is concerned you asked for a text file whose contents happen to look like gibberish.&lt;/p&gt;

&lt;p&gt;The other half of the grammar is the comma. It separates the header from the payload, and it is where hand-edited strings usually break. &lt;code&gt;data:image/png;base64&lt;/code&gt; with no comma is not a data URI at all; the string after it is just a suffix. If you feed that to a decoder that strips "everything up to the first comma", the strip silently does nothing and you end up decoding the header too — which fails immediately on &lt;code&gt;:&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt;, neither of which is in the Base64 alphabet.&lt;/p&gt;

&lt;p&gt;Nothing in that grammar is hard to type by hand, but the payload is where hand-built strings fail: a file has to be read as bytes and encoded without adding line breaks. &lt;a href="https://toolstead.net/blog/image-to-base64-javascript/" rel="noopener noreferrer"&gt;Image to Base64 in JavaScript&lt;/a&gt; covers the three ways to do it and which one keeps the wrapper valid.&lt;/p&gt;

&lt;p&gt;If you would rather not write the loop at all, &lt;a href="https://toolstead.net/image-to-base64/" rel="noopener noreferrer"&gt;build one from a file&lt;/a&gt; and pick the wrapper you need — Data URI, plain Base64 or the CSS form — with the character count and the 33% overhead shown next to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  The header is a claim, not evidence
&lt;/h1&gt;

&lt;p&gt;The media type in the header is a hint written by whoever produced the string. It is frequently wrong, and it is never checked by the browser when the image is rendered inline, because the browser trusts the &lt;code&gt;data:&lt;/code&gt; type it was given. That produces the most confusing class of bug in this area: a data URI that "works" but is wrong.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUg...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one renders in most browsers — the bytes are a PNG, and browsers sniff and recover — but the declared type is &lt;code&gt;image/jpeg&lt;/code&gt;. Save it as a file with a &lt;code&gt;.jpg&lt;/code&gt; extension and half your image tools will refuse to open it, and a build pipeline that trusts the header will mislabel it everywhere downstream.&lt;/p&gt;

&lt;p&gt;The robust approach is to read the &lt;strong&gt;signature&lt;/strong&gt; — the first few bytes — and treat the header as a claim to be verified rather than a fact. PNG always starts with &lt;code&gt;89 50 4E 47 0D 0A 1A 0A&lt;/code&gt;, JPEG with &lt;code&gt;FF D8 FF&lt;/code&gt;, GIF with &lt;code&gt;47 49 46 38&lt;/code&gt;, and WebP with &lt;code&gt;52 49 46 46&lt;/code&gt; followed by &lt;code&gt;57 45 42 50&lt;/code&gt; at offset 8. Those bytes are what the decoder actually uses. This is exactly what our &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; tool reports when the declared type and the real bytes disagree: it tells you both and renders the bytes.&lt;/p&gt;

&lt;h1&gt;
  
  
  The three bugs behind "it won't decode"
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;1. The payload is not Base64 anymore.&lt;/strong&gt; Copy the string out of a log file and you may bring the header along, or lose the padding, or pick up a URL-encoded &lt;code&gt;%3D&lt;/code&gt; where a &lt;code&gt;=&lt;/code&gt; was meant. A decoder that only accepts the 64-character alphabet will stop at the first illegal character and, if it is polite, tell you where: position, line and column, and the code point. If it just says "invalid input", you are left guessing. The &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; page exists for this case — it takes a payload with no prefix at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The payload is damaged in a way that changes its length.&lt;/strong&gt; Base64 encodes 3 bytes as 4 characters, so a valid string has a length divisible by 4 — or leaves a remainder of 2 or 3, which the &lt;code&gt;=&lt;/code&gt; padding fills out. A remainder of &lt;strong&gt;1&lt;/strong&gt; is impossible for a complete string. It means characters were lost, almost always because a line was truncated in a log, or a copy stopped early. No amount of guessing recovers the missing byte, and a decoder that "pads it anyway" is handing you a corrupted image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. It decoded perfectly — and it is not an image.&lt;/strong&gt; This is the one people least expect. Decode 200 kilobytes of perfect Base64 and you can end up with a gzip stream, a PDF, a ZIP archive, or a hexadecimal string that was already text. The giveaway is in the first bytes: &lt;code&gt;1F 8B&lt;/code&gt; is gzip, &lt;code&gt;25 50 44 46&lt;/code&gt; is PDF, &lt;code&gt;50 4B 03 04&lt;/code&gt; is a ZIP entry, and a payload made only of &lt;code&gt;0-9a-f&lt;/code&gt; is hex, not Base64 that happens to look odd. Gzipped JSON piped through a Base64 encoder is one of the most common things to find where you expected a photo.&lt;/p&gt;

&lt;h1&gt;
  
  
  Ten seconds of diagnosis you can do yourself
&lt;/h1&gt;

&lt;p&gt;If you have a browser console or a shell, three checks separate the cases. First, does the string even have a valid shape?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'data:image/png;base64,iVBORw0KGgo...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'has prefix:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'length % 4:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 1 means truncated&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'illegal chars:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Za&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;z0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;+/=&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, does it decode at all? In the browser, &lt;code&gt;atob&lt;/code&gt; throws on anything outside the alphabet — which makes it a stricter checker than &lt;code&gt;Buffer.from&lt;/code&gt; in Node, a point worth remembering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'decoded bytes:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'first 8:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'atob rejected it:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Third, are those first bytes a known image? &lt;code&gt;89 50&lt;/code&gt; is PNG, &lt;code&gt;ff d8&lt;/code&gt; is JPEG, &lt;code&gt;47 49&lt;/code&gt; is GIF, &lt;code&gt;52 49 46&lt;/code&gt; is WebP. If the first bytes are &lt;code&gt;1f 8b&lt;/code&gt; or &lt;code&gt;50 4b&lt;/code&gt;, you have a compressed archive, not a picture.&lt;/p&gt;

&lt;p&gt;In a shell the same three steps are one line each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s'&lt;/span&gt; &lt;span class="s2"&gt;"$B64"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; base64 -d &lt;span class="o"&gt;|&lt;/span&gt; xxd &lt;span class="o"&gt;|&lt;/span&gt; head -2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Decoding it properly, in each language
&lt;/h1&gt;

&lt;p&gt;The browser needs two steps because &lt;code&gt;atob&lt;/code&gt; returns a binary string, not bytes — and passing those characters straight into a &lt;code&gt;Blob&lt;/code&gt; corrupts anything above &lt;code&gt;0x7F&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;b64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'image/png'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node does it in one, but deliberately ignores characters it does not recognise, which is why it will happily "decode" a string with spaces or stray quotes in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// lenient: junk is skipped, not reported&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hex'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python is the strict member of the family again, and will raise rather than guess:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;
&lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# raises binascii.Error on bad input&lt;/span&gt;
&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'out.png'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'wb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want the padding repaired rather than the input rejected, pad explicitly instead of disabling validation — &lt;code&gt;payload + '=' * (-len(payload) % 4)&lt;/code&gt; — and only accept the result when the bytes afterwards still look like the image you expected.&lt;/p&gt;

&lt;h1&gt;
  
  
  Questions that come up every week
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Does a data URI avoid an HTTP request?&lt;/strong&gt; For the image itself, yes. But it is now part of whatever file contains it. Inside CSS, that means it blocks rendering with the stylesheet, cannot be cached separately, and is re-downloaded whenever the CSS changes. For a 2 KB icon that is a good trade; for a 300 KB photograph it is not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does the image get bigger?&lt;/strong&gt; Always, by about a third, because Base64 packs 3 bytes into 4 characters. Unlike text, already-compressed image bytes do not shrink under gzip, so that overhead does not come back — see &lt;a href="https://toolstead.net/blog/base64-overhead-33-percent/" rel="noopener noreferrer"&gt;why Base64 makes your page 33% bigger&lt;/a&gt; for the numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can a data URI carry a script?&lt;/strong&gt; In an &lt;code&gt;img&lt;/code&gt; element, no: browsers do not execute scripts inside images, including SVG. An SVG data URI used as a &lt;em&gt;document&lt;/em&gt; is a different story and is why you should not navigate to one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The prefix is missing entirely. Is the string still usable?&lt;/strong&gt; Usually yes, and that is what the bytes are for. Feed it to a decoder that reads the signature and the format comes from the bytes instead of the label — which is the only part of a data URI that cannot lie.&lt;/p&gt;

</description>
      <category>base64</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>"Duplicate URLs: Why One Page Gets Many Addresses"</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Wed, 18 Nov 2026 01:47:23 +0000</pubDate>
      <link>https://dev.to/toolstead/duplicate-urls-why-one-page-gets-many-addresses-g28</link>
      <guid>https://dev.to/toolstead/duplicate-urls-why-one-page-gets-many-addresses-g28</guid>
      <description>&lt;p&gt;A page does not have one address. It has as many addresses as clients can construct that resolve to it, and each of those is a separate key in every cache, log, analytics report and access rule that has ever seen it. Most of the confusion people blame on "caching" or "duplicate content" starts here.&lt;/p&gt;

&lt;p&gt;The specification is precise about which differences matter and which are cosmetic. Practice is less tidy, because servers, proxies and clients each normalise at different points — or not at all.&lt;/p&gt;

&lt;h1&gt;
  
  
  Six differences, and what each one does
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Trailing slash.&lt;/strong&gt; &lt;code&gt;example.com/about&lt;/code&gt; and &lt;code&gt;example.com/about/&lt;/code&gt; are different URLs as far as any cache or log is concerned. Whether they are the same &lt;em&gt;resource&lt;/em&gt; is decided by the server: it can redirect one to the other, serve the same content at both, or 404 one of them. There is no rule in the spec that settles it, only the server's behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case in the scheme and host.&lt;/strong&gt; &lt;code&gt;HTTPS://Example.COM/x&lt;/code&gt; and &lt;code&gt;https://example.com/x&lt;/code&gt; are equivalent, and hosts are case-insensitive by definition. Case in the &lt;em&gt;path&lt;/em&gt; is not: &lt;code&gt;/About&lt;/code&gt; and &lt;code&gt;/about&lt;/code&gt; may be two different pages, so normalisation must stop at the host boundary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default port.&lt;/strong&gt; &lt;code&gt;https://example.com:443/x&lt;/code&gt; and &lt;code&gt;https://example.com/x&lt;/code&gt; are the same address. The port rule is scheme-dependent — 443 for https, 80 for http — which is why a normaliser that strips "any port" breaks legitimate non-default ports.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Percent-encoding case.&lt;/strong&gt; &lt;code&gt;%2F&lt;/code&gt; and &lt;code&gt;%2f&lt;/code&gt; are the same octet: hex digits are case-insensitive inside a triplet. Your string comparison is not, so two URLs that a browser treats as identical can hash differently in your cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Over-encoding.&lt;/strong&gt; &lt;code&gt;%41&lt;/code&gt; is &lt;code&gt;A&lt;/code&gt;, and &lt;code&gt;A&lt;/code&gt; never needed encoding. Percent-encoding an unreserved character produces a second spelling of the same URL. RFC 3986 requires normalisation to decode those back, which matters because the &lt;em&gt;same&lt;/em&gt; rule says a &lt;code&gt;%2E&lt;/code&gt; that decodes to &lt;code&gt;.&lt;/code&gt; must not be allowed to change the path structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query order and repetition.&lt;/strong&gt; &lt;code&gt;?a=1&amp;amp;b=2&lt;/code&gt; and &lt;code&gt;?b=2&amp;amp;a=1&lt;/code&gt; are considered equivalent in the abstract, but no spec requires a server to treat them so, and most do not. Reordering parameters is safe for a cache and unsafe for a signature — there is no universally correct answer, only a decision you make and then apply everywhere. Repeating a parameter (&lt;code&gt;?a=1&amp;amp;a=2&lt;/code&gt;) is a third thing again: the spec says nothing, so each framework picks a rule, and "first wins" versus "last wins" is a real source of bugs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where the duplication actually comes from
&lt;/h1&gt;

&lt;p&gt;Most duplicate URLs are not user error. They are produced by the software:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A link generated with a slash and a form action posted without one.&lt;/li&gt;
&lt;li&gt;An email template that hardcoded &lt;code&gt;http://&lt;/code&gt; from an era when the site was not yet on TLS.&lt;/li&gt;
&lt;li&gt;A crawler that found &lt;code&gt;%2F&lt;/code&gt; in a sitemap and the plain slash in a menu.&lt;/li&gt;
&lt;li&gt;A tracking script appending parameters in a different order on every page load.&lt;/li&gt;
&lt;li&gt;A canonical tag pointing at one spelling while the internal links use another.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last one is the expensive one. A canonical tag is a &lt;em&gt;hint&lt;/em&gt; about which spelling you prefer; it does not stop the other spellings from being crawled, cached or counted. If your internal links disagree with your canonical, you have told a search engine which form you want while simultaneously producing the other.&lt;/p&gt;

&lt;h1&gt;
  
  
  The three layers, and which one owns what
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;The server owns the mapping.&lt;/strong&gt; A redirect from the non-canonical form to the canonical one is the only mechanism that actually removes the extra key from caches and browsers. A 301 or 308 is permanent, a 302 or 307 is temporary; the choice affects whether the old form keeps being requested.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The page owns the preference.&lt;/strong&gt; &lt;code&gt;rel="canonical"&lt;/code&gt; says which address you want indexed when several return the same content. It is advisory. It works best when it is consistent with the redirect, and worst when it contradicts it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The application owns the signature.&lt;/strong&gt; If you sign URLs — for uploads, for CDN access, for password-reset links — normalisation must happen &lt;em&gt;before&lt;/em&gt; signing, and every component must normalise identically. Signing a reordered query string produces a signature that fails on a server that sorted it, and the error message will say "invalid signature", not "you sorted the parameters".&lt;/p&gt;

&lt;h1&gt;
  
  
  A practical rule set
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick one form per resource and write it down.&lt;/strong&gt; Trailing slash or not, &lt;code&gt;www&lt;/code&gt; or not, lowercase paths or not — decide once, and treat deviations as bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redirect every other form, at the edge.&lt;/strong&gt; One 308 rule beats fifty canonical tags. Our own site ends up doing exactly this: &lt;code&gt;/x&lt;/code&gt; answers with a redirect to &lt;code&gt;/x/&lt;/code&gt;, and &lt;code&gt;/x.html&lt;/code&gt; with a redirect to &lt;code&gt;/x&lt;/code&gt;. The cost is visible in server logs as a steady trickle of 308s from clients that only ever heard the shorter spelling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Normalise before you compare or sign.&lt;/strong&gt; Decode unreserved characters, uppercase your triplets, drop the default port — then hash. If you must support reordered queries, sort them at the point of signing &lt;em&gt;and&lt;/em&gt; at the point of verification.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do not rely on search engines to merge anything quickly.&lt;/strong&gt; They do merge, eventually, and slowly. In the meantime the extra spellings accumulate impressions, split click-through, and make your own reporting wrong.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What it looks like when it goes wrong
&lt;/h1&gt;

&lt;p&gt;The symptom is rarely an error page. It is a report where the same article has two rows, one with fifty impressions and one with thirty-five, and a total that matches neither. Or a cache with a 4% hit rate on a page that is requested constantly. Or an access rule that blocks &lt;code&gt;/admin/&lt;/code&gt; while &lt;code&gt;/admin&lt;/code&gt; is served happily.&lt;/p&gt;

&lt;p&gt;The same discipline shows up in smaller places. When a token arrives truncated at an ampersand, the cause is an unencoded separator — the subject of &lt;a href="https://toolstead.net/blog/percent-encoding-reserved-characters/" rel="noopener noreferrer"&gt;which characters actually need encoding&lt;/a&gt;. When a Base64 value breaks after a URL round trip, the cause is the &lt;code&gt;+&lt;/code&gt; that became a space — covered in &lt;a href="https://toolstead.net/blog/url-encoding-vs-base64/" rel="noopener noreferrer"&gt;URL encoding versus Base64&lt;/a&gt;, along with the URL-safe alphabet that &lt;a href="https://toolstead.net/blog/url-safe-base64-vs-standard/" rel="noopener noreferrer"&gt;replaces &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt;&lt;/a&gt;. Different symptoms, one root cause: the same bytes written two ways.&lt;/p&gt;

&lt;p&gt;When you are staring at two spellings of one URL, the fastest way to see whether they differ in the escapes or in the text is to &lt;a href="https://toolstead.net/url-encoder/" rel="noopener noreferrer"&gt;decode the percent-escapes and compare&lt;/a&gt;: it converts as you type and keeps &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;%20&lt;/code&gt; apart, which is where most of these duplicates come from in the first place.&lt;/p&gt;

&lt;p&gt;Normalisation is the discipline of deciding which way, once, and then making every layer agree.&lt;/p&gt;

</description>
      <category>seo</category>
      <category>webdev</category>
      <category>http</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Convert an Image to Base64 in JavaScript (3 Ways)</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Tue, 17 Nov 2026 01:43:25 +0000</pubDate>
      <link>https://dev.to/toolstead/how-to-convert-an-image-to-base64-in-javascript-3-ways-3pdo</link>
      <guid>https://dev.to/toolstead/how-to-convert-an-image-to-base64-in-javascript-3-ways-3pdo</guid>
      <description>&lt;p&gt;There are three common ways to turn an image into Base64 in a browser, and they are not interchangeable. Two of them preserve your bytes exactly and one quietly re-encodes the image; one of them cannot be used on a file the user has not chosen; and on large files one of them will blow the call stack if you write it the obvious way.&lt;/p&gt;

&lt;p&gt;Here is what each one actually does, and when to reach for it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Way 1: FileReader, for bytes you must not touch
&lt;/h1&gt;

&lt;p&gt;If you want the exact bytes of the file the user selected — no re-compression, no metadata loss — &lt;code&gt;FileReader.readAsDataURL&lt;/code&gt; is the tool. It reads the file, Base64-encodes it, and hands you a complete data URI including the prefix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fileToDataUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// "data:image/png;base64,iVBOR..."&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readAsDataURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fileToDataUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'img'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note what &lt;code&gt;reader.result&lt;/code&gt; contains: the whole data URI, prefix included. If your API expects only the payload, split on the first comma — and be aware that the declared type comes from the file's &lt;code&gt;type&lt;/code&gt; property, which is derived from the extension and can be wrong. For an API, it is worth verifying the signature against the bytes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FileReader&lt;/code&gt; streams the file rather than materialising it as an array first, so it is the friendliest of the three to memory. It accepts &lt;code&gt;Blob&lt;/code&gt; and &lt;code&gt;File&lt;/code&gt; objects, which means it also works on data you generated yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'application/json'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fileToDataUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// works on any Blob, not just user files&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Way 2: canvas, when you want the pixels rather than the file
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;canvas.toDataURL()&lt;/code&gt; re-encodes the image through the browser's own encoder. That is what you want if you are resizing, cropping, or normalising a format — and exactly what you must avoid if you care about the original file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reencode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createImageBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxWidth&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'canvas'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'2d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDataURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'image/jpeg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four consequences worth knowing before you use it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The bytes change.&lt;/strong&gt; A JPEG goes through a decode and a fresh encode, so it loses a generation of quality every time you do this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metadata is dropped.&lt;/strong&gt; EXIF — camera model, orientation, GPS coordinates — does not survive a canvas round trip. That is sometimes the point, and sometimes a silent data loss that matters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;quality&lt;/code&gt; only applies to lossy formats.&lt;/strong&gt; It is ignored for &lt;code&gt;image/png&lt;/code&gt;; passing 0.1 to a PNG produces a file the same size as 1.0.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The output type is whatever you ask for&lt;/strong&gt;, not what came in. &lt;code&gt;toDataURL('image/png')&lt;/code&gt; on a JPEG produces a PNG, several times larger.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alpha matters here too: converting a transparent PNG to &lt;code&gt;image/jpeg&lt;/code&gt; fills transparency with black, because JPEG has no alpha channel.&lt;/p&gt;

&lt;h1&gt;
  
  
  Way 3: raw bytes, when you need the payload without the prefix
&lt;/h1&gt;

&lt;p&gt;Sometimes you want the Base64 payload on its own, or you want to build the data URI yourself so you control the declared type. Read the file as an array buffer and encode it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fileToBase64Payload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;arrayBuffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;bytesToBase64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The naive implementation of that last step is where people get burned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✗ breaks on large files&lt;/span&gt;
&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;String.fromCharCode(...bytes)&lt;/code&gt; spreads every byte into an argument list, and argument lists have a length limit — in practice around 65,535 in most engines. On a 200 KB image you get &lt;code&gt;RangeError: Maximum call stack size exceeded&lt;/code&gt;, which reads like a recursion bug and is actually an argument-count limit.&lt;/p&gt;

&lt;p&gt;The fix is to process the buffer in chunks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bytesToBase64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;CHUNK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                      &lt;span class="c1"&gt;// 32 KiB of bytes per call&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="no"&gt;CHUNK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;CHUNK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two more details that only show up with real data. First, &lt;code&gt;btoa&lt;/code&gt; operates on code units, not bytes — feeding it a string that contains anything above &lt;code&gt;0x7F&lt;/code&gt; throws &lt;code&gt;InvalidCharacterError&lt;/code&gt;, which is why the byte-to-string step above uses &lt;code&gt;fromCharCode&lt;/code&gt; on raw bytes rather than on decoded text. Second, the reverse direction has the same trap in mirror image: &lt;code&gt;Uint8Array.from(atob(s), c =&amp;gt; c.charCodeAt(0))&lt;/code&gt; is correct, while treating the result of &lt;code&gt;atob&lt;/code&gt; as text and passing it to a &lt;code&gt;Blob&lt;/code&gt; corrupts every byte above &lt;code&gt;0x7F&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Which one to use
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uploading or transmitting an image without altering it&lt;/strong&gt; → &lt;code&gt;FileReader&lt;/code&gt; or raw bytes. Never canvas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Previewing a local file&lt;/strong&gt; → an object URL beats Base64 entirely: see &lt;a href="https://toolstead.net/blog/data-uri-vs-blob-url/" rel="noopener noreferrer"&gt;data URI vs blob URL&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shrinking, cropping, or converting the format&lt;/strong&gt; → canvas. Remember it is a re-encode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generating a small asset to embed in CSS or HTML&lt;/strong&gt; → canvas for generated pixels, and then read &lt;a href="https://toolstead.net/blog/base64-overhead-33-percent/" rel="noopener noreferrer"&gt;why Base64 makes the page bigger&lt;/a&gt; before you inline a photograph.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The memory cost of the string form
&lt;/h1&gt;

&lt;p&gt;Base64 output is about 1.37 times the size of the input bytes, and in JavaScript that string is stored as UTF-16 — two bytes per character. The multiplication is worth doing once, because it explains why "just convert it in the browser" stops working at a certain file size:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 MB image&lt;/strong&gt; → ~1.37 MB Base64 string → &lt;strong&gt;~2.7 MB&lt;/strong&gt; in memory as UTF-16&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10 MB image&lt;/strong&gt; → ~13.7 MB Base64 string → &lt;strong&gt;~27 MB&lt;/strong&gt; in memory&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;100 MB image&lt;/strong&gt; → ~137 MB Base64 string → &lt;strong&gt;~274 MB&lt;/strong&gt; in memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add the original buffer, which you are usually still holding, and a 10 MB image costs roughly 37 MB at the moment of conversion. That is survivable on a desktop and uncomfortable on a phone, which is why the encode side of our tool accepts images up to 100 MB for &lt;em&gt;previewing&lt;/em&gt; but keeps the generated string out of the DOM — a textarea holding 137 MB of UTF-16 is the specific failure mode that makes mobile browsers kill the tab.&lt;/p&gt;

&lt;p&gt;If you must handle very large images, process them as blobs and never build the whole string: upload the &lt;code&gt;Blob&lt;/code&gt; directly, or stream it in chunks and encode each chunk separately. Base64 is convenient precisely because it is text, and text is the expensive representation.&lt;/p&gt;

&lt;h1&gt;
  
  
  About building the string for a specific consumer
&lt;/h1&gt;

&lt;p&gt;The prefix is a claim, and different consumers want different shapes. A CSS &lt;code&gt;url()&lt;/code&gt; needs the full data URI in quotes; an &lt;code&gt;&amp;lt;img src&amp;gt;&lt;/code&gt; needs the same string; a JSON API usually wants the bare payload plus a separate content type; an email template often wants the payload wrapped at 76 characters per line. Our &lt;a href="https://toolstead.net/image-to-base64/" rel="noopener noreferrer"&gt;Image to Base64&lt;/a&gt; tool exists mainly to produce those variants — including URL-safe output, line-wrapped output, and the surrounding quotes — because getting the wrapper wrong is a more common failure than getting the encoding wrong. If you are generating the string in code, write a test that decodes your own output and compares it byte for byte with the input; that single assertion catches every wrapper mistake listed above.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>base64</category>
    </item>
    <item>
      <title>Base64 vs Hex, Base32 and ASCII85: Encodings Compared</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Mon, 16 Nov 2026 01:37:42 +0000</pubDate>
      <link>https://dev.to/toolstead/base64-vs-hex-base32-and-ascii85-encodings-compared-2f22</link>
      <guid>https://dev.to/toolstead/base64-vs-hex-base32-and-ascii85-encodings-compared-2f22</guid>
      <description>&lt;p&gt;Base64 is the default choice for turning bytes into text, but it is not the only one and it is not always the right one. Hexadecimal is easier to read and worse at everything else. Base32 is bigger and survives environments that destroy case. ASCII85 is smaller and appears in places you would not expect. Each exists because of a specific constraint, and picking the wrong one is usually a decision nobody made on purpose.&lt;/p&gt;

&lt;h1&gt;
  
  
  The four, side by side
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hex&lt;/strong&gt; — alphabet &lt;code&gt;0-9a-f&lt;/code&gt;, 6 characters per 3 bytes, &lt;strong&gt;+100%&lt;/strong&gt; overhead, case-insensitive&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base32&lt;/strong&gt; — alphabet &lt;code&gt;A-Z2-7&lt;/code&gt;, 8 characters per 5 bytes, &lt;strong&gt;+60%&lt;/strong&gt; overhead, case-insensitive (uppercase by convention)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base64&lt;/strong&gt; — alphabet &lt;code&gt;A-Za-z0-9+/&lt;/code&gt;, 4 characters per 3 bytes, &lt;strong&gt;+33%&lt;/strong&gt; overhead, &lt;strong&gt;case-sensitive&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ASCII85&lt;/strong&gt; — alphabet &lt;code&gt;!&lt;/code&gt; to &lt;code&gt;u&lt;/code&gt;, 5 characters per 4 bytes, &lt;strong&gt;+25%&lt;/strong&gt; overhead, case-sensitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The overhead figures are arithmetic, not preference. Hex spends exactly two characters per byte, so it doubles the data. Base32 packs 5 bits per character, so it needs 8 characters to carry the 40 bits of 5 bytes — 60% overhead. Base64 packs 6 bits into 4 characters for 3 bytes. ASCII85 packs 4 bytes into 5 characters, the most efficient of the four; it is what PostScript and PDF use for embedded streams.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hex: for humans and for hashes
&lt;/h1&gt;

&lt;p&gt;Hex is the least efficient encoding here and the most useful one when a person is going to look at the data. Every byte maps to a fixed pair of characters, so the mapping is memorisable and offsets are trivially readable: byte 4 of a file is characters 8 and 9 in the hex dump, no arithmetic required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That property is why hashes, checksums, MAC addresses, UUIDs and file signatures are all written in hex. When you read &lt;code&gt;89 50 4E 47&lt;/code&gt; at the start of a file, you are reading PNG's signature in the form the format specification uses, and there is no translation step between the documentation and your hex dump.&lt;/p&gt;

&lt;p&gt;The failure mode to know: &lt;strong&gt;hex is not Base64, and decoders rarely complain when you confuse them.&lt;/strong&gt; A string of &lt;code&gt;0-9a-f&lt;/code&gt; is a subset of both alphabets, so &lt;code&gt;Buffer.from(hexString, 'base64')&lt;/code&gt; returns bytes without error — just not the bytes you meant. It is one of the few cases where a decoder succeeds and the answer is simply wrong, which makes it worth checking before anything else when a payload "decodes but makes no sense".&lt;/p&gt;

&lt;h1&gt;
  
  
  Base32: when case cannot be trusted
&lt;/h1&gt;

&lt;p&gt;Base32 uses 32 uppercase letters and digits, and it exists for environments where case is destroyed or confusable: DNS names, some barcode and QR workflows, serial numbers read aloud, and systems documented as case-insensitive. Its 60% overhead is the price of removing ambiguity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b32encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# NBSWY3DP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The alphabet also excludes &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;8&lt;/code&gt; and &lt;code&gt;9&lt;/code&gt;, along with &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;L&lt;/code&gt;, &lt;code&gt;O&lt;/code&gt; and &lt;code&gt;U&lt;/code&gt; in the extended variant, which removes the character pairs people misread. If a human has to transcribe a token by hand, that matters more than the extra 27 percentage points of size. If a machine is transferring it, base32 is a strictly worse choice than Base64.&lt;/p&gt;

&lt;h1&gt;
  
  
  Base64: the default, with two alphabets
&lt;/h1&gt;

&lt;p&gt;The reason Base64 dominates is that it hits the best available compromise: 6 bits per character is the most you can carry while keeping every character printable, and 64 is the largest power of two that fits in the printable ASCII range. Its two problems — &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt; colliding with URL syntax, and case sensitivity — are solved by the URL-safe variant and by knowing your transport.&lt;/p&gt;

&lt;p&gt;The alphabet itself is the source of most Base64 pain in practice: a single copied &lt;code&gt;l&lt;/code&gt; for &lt;code&gt;I&lt;/code&gt;, or &lt;code&gt;O&lt;/code&gt; for &lt;code&gt;0&lt;/code&gt;, produces a string that decodes without complaint into the wrong bytes. That is why implementations report the character position and code point when they reject input, and why "check the code point" is better advice than "check the character" when two symbols look alike.&lt;/p&gt;

&lt;h1&gt;
  
  
  ASCII85: smaller, and mostly invisible
&lt;/h1&gt;

&lt;p&gt;ASCII85 encodes 4 bytes as 5 characters using a 85-symbol alphabet covering &lt;code&gt;!&lt;/code&gt; through &lt;code&gt;u&lt;/code&gt;, giving 25% overhead instead of Base64's 33%. The saving is real but modest, and the cost is a much less forgiving format: no padding mechanism in the usual sense, special rules for long runs of zero bytes, and an alphabet that includes characters needing escaping in PostScript and XML.&lt;/p&gt;

&lt;p&gt;You are most likely to meet it inside a PDF or a PostScript file rather than to choose it. If you do need to decode one, Python has it built in — &lt;code&gt;base64.a85decode&lt;/code&gt; — and it is worth knowing that PDF also defines &lt;code&gt;ASCIIHexDecode&lt;/code&gt;, so a PDF stream filter chart is a place where three of these four encodings appear side by side.&lt;/p&gt;

&lt;h1&gt;
  
  
  Choosing, in one paragraph
&lt;/h1&gt;

&lt;p&gt;Use &lt;strong&gt;hex&lt;/strong&gt; when a person or a specification will read the bytes, and when fixed-width readability matters more than size: hashes, signatures, binary protocol dumps, colour values. Use &lt;strong&gt;Base64&lt;/strong&gt; for everything else that moves through text — JSON, XML, email, HTTP bodies, data URIs, embedded assets — and switch to its URL-safe alphabet when the string will appear in a URL, a filename or a cookie. Reach for &lt;strong&gt;Base32&lt;/strong&gt; only when case sensitivity or human transcription is the binding constraint. Choose &lt;strong&gt;ASCII85&lt;/strong&gt; when you are implementing a format that already uses it, and not otherwise.&lt;/p&gt;

&lt;p&gt;One caveat on that URL advice: the alphabet takes care of &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt;, but not of the characters that have a job in a URL. In a query parameter the reserved set matters more than the alphabet — see &lt;a href="https://toolstead.net/blog/percent-encoding-reserved-characters/" rel="noopener noreferrer"&gt;percent-encoding&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Converting between them
&lt;/h1&gt;

&lt;p&gt;All four are lossless round trips, so conversions are safe — but each conversion is a place where a mistake hides, because every one of these encodings produces strings that look plausible in the other alphabets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;toHex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hex'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;toB64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;toB64Url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64url'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;fromHex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'hex'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;fromB64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// a hex string decoded as base64: no error, wrong bytes&lt;/span&gt;
&lt;span class="nf"&gt;fromB64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'utf8'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// not 'hello'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The safest habit is to convert bytes, never strings: decode to bytes, then re-encode from those bytes. Converting a hex string directly to Base64 by string manipulation works, but it silently assumes both encodings are in their canonical form — no whitespace, no line breaks, correct padding — and the assumption fails exactly when something upstream did something unusual.&lt;/p&gt;

&lt;p&gt;To see the size difference on your own text instead of in a table, &lt;a href="https://toolstead.net/ascii85-encoder/" rel="noopener noreferrer"&gt;encode the same text as Ascii85&lt;/a&gt; and put it next to the Base64 of the same bytes: the 25% against 33%, the &lt;code&gt;&amp;lt;~ ~&amp;gt;&lt;/code&gt; wrapper and the line-wrapping at 75 characters are all visible in one glance.&lt;/p&gt;

&lt;p&gt;If you want to see what a given payload actually is before choosing a decoder, the answer usually comes from the first few bytes rather than the alphabet: a hex-looking string that starts with &lt;code&gt;89 50&lt;/code&gt; when decoded as hex is a PNG, and &lt;a href="https://toolstead.net/blog/image-file-signatures/" rel="noopener noreferrer"&gt;image file signatures&lt;/a&gt; lists the rest. And if the goal is simply to see the image, &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; will try the plausible interpretations in order and tell you which one worked.&lt;/p&gt;

&lt;p&gt;One more comparison is worth carrying in your head, because it decides how much size matters: Base64 and ASCII85 are for moving arbitrary bytes through text, hex is for &lt;em&gt;reading&lt;/em&gt; bytes, and Base32 is for &lt;em&gt;transcribing&lt;/em&gt; them. Only the first two are about efficiency, and between them the 8-point difference (33% versus 25%) is rarely the deciding factor — the surrounding format usually is. If you are fighting size rather than encoding, the more useful question is whether the data should be text at all; &lt;a href="https://toolstead.net/blog/base64-overhead-33-percent/" rel="noopener noreferrer"&gt;why Base64 makes files 33% bigger&lt;/a&gt; covers where inlining stops paying for itself.&lt;/p&gt;

</description>
      <category>base64</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>URL Encoding vs Base64: When to Use Which</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Sat, 14 Nov 2026 06:07:50 +0000</pubDate>
      <link>https://dev.to/toolstead/url-encoding-vs-base64-when-to-use-which-1na4</link>
      <guid>https://dev.to/toolstead/url-encoding-vs-base64-when-to-use-which-1na4</guid>
      <description>&lt;p&gt;Both percent-encoding and Base64 turn input into a restricted set of ASCII characters, both are reversible, and both are routinely described as "making a string safe". They are also answers to completely different questions, which is why choosing between them by feel produces data that survives the round trip in testing and falls apart in a log file three weeks later.&lt;/p&gt;

&lt;p&gt;The distinction is one sentence long: &lt;strong&gt;percent-encoding protects characters that have syntactic meaning inside a URL, and Base64 re-encodes bytes that are not text at all.&lt;/strong&gt; Get that backwards and nothing throws an exception — the value simply arrives different, or half of it arrives.&lt;/p&gt;

&lt;h1&gt;
  
  
  The rule, stated once
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;If what you have is &lt;strong&gt;text&lt;/strong&gt; that must sit inside a URL component (a search term, a filename, a redirect target), &lt;strong&gt;percent-encode it&lt;/strong&gt; for that component.&lt;/li&gt;
&lt;li&gt;If what you have is &lt;strong&gt;bytes&lt;/strong&gt; (an image, a hash, an encryption key, a protobuf), &lt;strong&gt;Base64 them&lt;/strong&gt; — and then percent-encode the Base64 if it has to travel in a URL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second clause is the one people skip, and it is the source of the classic bug covered below. The two transforms compose; they do not compete.&lt;/p&gt;

&lt;h1&gt;
  
  
  What each one actually guarantees
&lt;/h1&gt;

&lt;p&gt;Percent-encoding is defined &lt;em&gt;per component of the URL grammar&lt;/em&gt;. It says: replace the bytes that are not allowed here with &lt;code&gt;%&lt;/code&gt; followed by two hex digits. Its output is still text with the same meaning — a human can read an encoded path and see what it was. Its cost is unbounded in the worst case: an ASCII character costs one &lt;code&gt;%XX&lt;/code&gt; triplet if it needs escaping (three characters), and a character outside ASCII costs one triplet per UTF-8 byte, so a single CJK character is nine characters.&lt;/p&gt;

&lt;p&gt;Base64 is defined over &lt;strong&gt;bytes&lt;/strong&gt;, with no knowledge of URLs. Its output uses 64 characters — &lt;code&gt;A&lt;/code&gt;–&lt;code&gt;Z&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;–&lt;code&gt;z&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;–&lt;code&gt;9&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; — plus &lt;code&gt;=&lt;/code&gt; padding, and it expands by exactly one third. All of those characters are legal in a URL &lt;em&gt;as characters&lt;/em&gt;, which is exactly why Base64 breaks there: &lt;code&gt;+&lt;/code&gt; means something else, &lt;code&gt;/&lt;/code&gt; means something else, and &lt;code&gt;=&lt;/code&gt; is used by other parts of the URL syntax. Legal, and load-bearing, are not the same property.&lt;/p&gt;

&lt;h1&gt;
  
  
  The failure everyone hits: Base64 in a query string
&lt;/h1&gt;

&lt;p&gt;Take an image encoded as standard Base64 and dropped into a query parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.test/img?data=iVBORw0KGgoAAAANSUhEUg...+/8AAAAASUVORK5CYII=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every character there is legal in a URL, so the browser sends it and the server receives it. Then the server parses the query string with &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; rules — which is what most frameworks do by default — and in those rules &lt;code&gt;+&lt;/code&gt; means a &lt;strong&gt;space&lt;/strong&gt;. The Base64 that arrives is not the Base64 that was sent. If it decodes at all, it decodes to different bytes; more often it fails at a length that is not a multiple of four, at a totally different character position from the one that was altered.&lt;/p&gt;

&lt;p&gt;There are two correct fixes, and they are not interchangeable:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Percent-encode the Base64&lt;/strong&gt; before appending it, and decode in the reverse order on the way back. &lt;code&gt;+&lt;/code&gt; becomes &lt;code&gt;%2B&lt;/code&gt;, which form parsing leaves alone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use the URL-safe alphabet&lt;/strong&gt; — &lt;code&gt;-&lt;/code&gt; and &lt;code&gt;_&lt;/code&gt; instead of &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt; — which removes the ambiguity at the source rather than escaping around it. That alphabet, and where it comes from, is covered in &lt;a href="https://toolstead.net/blog/url-safe-base64-vs-standard/" rel="noopener noreferrer"&gt;URL-safe Base64 vs standard Base64&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The order of operations is worth writing down, because it is easy to get one step wrong and hard to see: &lt;code&gt;bytes → Base64 → percent-encode → query string&lt;/code&gt;, and in reverse &lt;code&gt;query string → percent-decode → Base64-decode → bytes&lt;/code&gt;. A percent-decode after a Base64-decode is a bug that only shows up on inputs containing &lt;code&gt;%&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  encodeURI vs encodeURIComponent
&lt;/h1&gt;

&lt;p&gt;JavaScript offers both, and the difference is exactly the "per component" rule.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;encodeURIComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a&amp;amp;b=c d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// 'a%26b%3Dc%20d'&lt;/span&gt;
&lt;span class="nf"&gt;encodeURI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://x.test/?a&amp;amp;b=c d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'https://x.test/?a&amp;amp;b=c%20d'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;encodeURI&lt;/code&gt; is designed to take a &lt;strong&gt;whole URL you already assembled&lt;/strong&gt;. It therefore leaves &lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;:&lt;/code&gt; alone — they are the URL's structure, and encoding them would destroy it. Used on a value that is then pasted into a query parameter, it leaves the &lt;code&gt;&amp;amp;&lt;/code&gt; alone, and the &lt;code&gt;&amp;amp;&lt;/code&gt; splits your value into a second parameter. That is a straightforward injection: user text becomes URL syntax.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;encodeURIComponent&lt;/code&gt; is designed for &lt;strong&gt;one piece of a URL&lt;/strong&gt;, so it encodes everything structural. It is the function to reach for by default, and one of the few places a rule of thumb is genuinely correct: if you are not certain, encode harder — an over-encoded unreserved character is harmless, an under-encoded &lt;code&gt;&amp;amp;&lt;/code&gt; is a parameter injection.&lt;/p&gt;

&lt;p&gt;Note also what &lt;code&gt;encodeURIComponent&lt;/code&gt; does not encode: &lt;code&gt;! ' ( ) * - . _ ~&lt;/code&gt; and alphanumerics are left as-is, because RFC 3986 marks them as unreserved. That is correct, and it is why two different producers can emit different-looking but equivalent strings — a fact that matters later, when a signature or cache key is computed over one of them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Plus and space, the rule behind the folklore
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt; meaning space is not a URL rule. It is a rule of &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt;, the format used by HTML forms and, by convention, by query strings on many servers. Consequences worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a &lt;strong&gt;query string&lt;/strong&gt;, &lt;code&gt;+&lt;/code&gt; is usually read as a space. In a &lt;strong&gt;path&lt;/strong&gt;, it is a literal plus. So &lt;code&gt;?q=C%2B%2B&lt;/code&gt; and &lt;code&gt;?q=C++&lt;/code&gt; are different on a standards-compliant parser, and &lt;code&gt;%2B&lt;/code&gt; is the only reliable way to send a literal plus in a value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;encodeURIComponent&lt;/code&gt; produces &lt;code&gt;%20&lt;/code&gt; for a space, not &lt;code&gt;+&lt;/code&gt;. &lt;code&gt;new URLSearchParams({ q: 'a b' }).toString()&lt;/code&gt; produces &lt;code&gt;q=a+b&lt;/code&gt; — same meaning, different spelling. Both decode correctly under a form parser; neither decodes correctly under a strict URL parser that treats &lt;code&gt;+&lt;/code&gt; literally. Which one you get is decided by the library, not by you, which is why round-tripping a value through two libraries is a reliable way to grow an extra space.&lt;/li&gt;
&lt;li&gt;When a value is &lt;em&gt;already&lt;/em&gt; encoded and you encode it again, spaces and &lt;code&gt;+&lt;/code&gt; do not survive a naive double transform. That is the topic of &lt;a href="https://toolstead.net/blog/percent-encoding-reserved-characters/" rel="noopener noreferrer"&gt;percent-encoding: the reserved characters that break URLs&lt;/a&gt;, which goes through the character-level rules this section summarises.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  When neither belongs in a URL
&lt;/h1&gt;

&lt;p&gt;Both transform encodings cost something, and neither is a solution to "the URL is too long". Percent-encoding can triple the length of ASCII input and worse for non-ASCII; Base64 adds a flat 33% and then gets percent-encoded, so a binary payload in a URL can end up nearly twice its size in characters before anything else is counted — see &lt;a href="https://toolstead.net/blog/base64-overhead-33-percent/" rel="noopener noreferrer"&gt;why Base64 costs 33%&lt;/a&gt; for the arithmetic.&lt;/p&gt;

&lt;p&gt;Practical limits are the reason this matters: older browsers capped URLs near 2,083 characters, many servers and proxies cap the request line at 8 KB, and every URL ends up in logs, analytics and referrer headers, where a megabyte of encoded data is not something anyone wants to retain. If the payload is more than a token or a short parameter, it belongs in the request body, not in the query string. If it is binary and it must be in a URL — a signed thumbnail, an inline data URI — that is a design decision with a cost, and Base64 plus percent-encoding is that cost. If you want to see where that cost lands, &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;decode the data URI back into an image&lt;/a&gt; — the decoded byte count is printed next to the payload you pasted.&lt;/p&gt;

&lt;h1&gt;
  
  
  The decision list
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;User text going into a URL component → &lt;code&gt;encodeURIComponent&lt;/code&gt;, or the equivalent in your language.&lt;/li&gt;
&lt;li&gt;An assembled URL that needs non-ASCII or spaces escaped → &lt;code&gt;encodeURI&lt;/code&gt;, knowing it leaves structure intact.&lt;/li&gt;
&lt;li&gt;Binary going through a text channel → Base64; if the destination is a URL, use the URL-safe alphabet or percent-encode the result.&lt;/li&gt;
&lt;li&gt;Text going into a form field or a body → percent-encode only if the field is defined as URL-encoded; otherwise send it as-is.&lt;/li&gt;
&lt;li&gt;Anything large → the body, not the URL.&lt;/li&gt;
&lt;li&gt;Base64 inside a JSON string inside a URL — the case that catches everyone twice — encode the Base64 first, then the JSON, then the URL component, and decode in exact reverse. &lt;a href="https://toolstead.net/url-encoder/" rel="noopener noreferrer"&gt;The URL encoder and decoder&lt;/a&gt; shows the character-by-character result of each step, which is the fastest way to see which layer is eating your &lt;code&gt;+&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>http</category>
      <category>beginners</category>
    </item>
    <item>
      <title>"MIFARE Classic Sector Dump: What the Hex Actually Says"</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Fri, 13 Nov 2026 01:47:52 +0000</pubDate>
      <link>https://dev.to/toolstead/mifare-classic-sector-dump-what-the-hex-actually-says-2m4k</link>
      <guid>https://dev.to/toolstead/mifare-classic-sector-dump-what-the-hex-actually-says-2m4k</guid>
      <description>&lt;p&gt;A card dump is the only description of a credential that does not depend on trusting somebody's reader. Every other form — the number printed on the card, the value the panel shows, the frame on the wire — has already been through a conversion that somebody chose. The dump has not. It is the bytes as they sit in the card's memory.&lt;/p&gt;

&lt;p&gt;That also makes it the form people misread most often, because nothing in it is labelled and three of its conventions run against intuition.&lt;/p&gt;

&lt;h1&gt;
  
  
  The 1K layout, in one paragraph
&lt;/h1&gt;

&lt;p&gt;A MIFARE Classic 1K card holds 1024 bytes, organised as &lt;strong&gt;16 sectors of 4 blocks&lt;/strong&gt;, and each block is &lt;strong&gt;16 bytes&lt;/strong&gt;. Blocks are addressed from 0 to 63. In every sector, the last block is special: block 3 of sector 0, block 7 of sector 1, block 11 of sector 2, and so on. Data lives in the other three blocks of each sector; the fourth holds the keys and the permissions for that sector.&lt;/p&gt;

&lt;p&gt;So a full dump is 64 lines of 16 bytes if your tool prints one block per line, or 1024 bytes of continuous hex if it does not. When a dump arrives as one long string with no line breaks, the first thing to do is re-split it at 16 bytes — otherwise every offset you compute from it will be wrong, and wrong in a way that still produces plausible-looking numbers.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sector 0, block 0 is not like the others
&lt;/h1&gt;

&lt;p&gt;The first block of the first sector is the manufacturer block, and it is read-only. Its bytes are laid out roughly like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bytes 0 to 3 (or 0 to 6, or 0 to 9): the UID&lt;/li&gt;
&lt;li&gt;the byte after the UID: the BCC, a checksum computed as the XOR of the UID bytes&lt;/li&gt;
&lt;li&gt;the next byte: SAK&lt;/li&gt;
&lt;li&gt;the next two bytes: ATQA&lt;/li&gt;
&lt;li&gt;the rest: manufacturer data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two consequences follow. First, &lt;strong&gt;you cannot change a card's UID by writing to it&lt;/strong&gt; — that block refuses writes, which is why "UID changeable" cards are a separate product with a different chip inside, not a setting. Second, a 4-byte UID and a 7-byte UID produce different layouts inside the same block, so a script that hardcodes offsets 0 to 3 will read a 7-byte card's UID as "the first four bytes" and silently drop three.&lt;/p&gt;

&lt;h1&gt;
  
  
  The UID is stored backwards
&lt;/h1&gt;

&lt;p&gt;This is the part that costs people the most time. MIFARE stores the UID &lt;strong&gt;least significant byte first&lt;/strong&gt;, while most readers, panels and printed card numbers show it most significant byte first. A card whose dump begins &lt;code&gt;E1 5A 3C 04&lt;/code&gt; is the card a reader calls &lt;code&gt;043C5AE1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Anyone who has compared two readers already knows this failure mode from the other direction — see &lt;a href="https://toolstead.net/blog/card-number-formats-explained/" rel="noopener noreferrer"&gt;why card number formats differ&lt;/a&gt; for the same problem in decimal and in 125 kHz readers. The dump does not have a convention of its own; it simply preserves the chip's byte order, and every layer above it may or may not reverse that order before showing you a number.&lt;/p&gt;

&lt;p&gt;The BCC byte is a useful check here. XOR the UID bytes together and compare: if the result does not match the byte following the UID, either the UID length is not what you assumed, or the dump is not the block you think it is. It is a one-line check that catches the most common misreading.&lt;/p&gt;

&lt;h1&gt;
  
  
  The trailer block is three fields, not one
&lt;/h1&gt;

&lt;p&gt;The last block of each sector contains, in order: &lt;strong&gt;6 bytes of Key A&lt;/strong&gt;, &lt;strong&gt;4 bytes of access bits&lt;/strong&gt;, &lt;strong&gt;1 general-purpose byte&lt;/strong&gt;, and &lt;strong&gt;6 bytes of Key B&lt;/strong&gt;. That is 17 fields' worth of meaning packed into 16 bytes, and the reason a "key" is never a 16-byte value no matter how often it is described that way.&lt;/p&gt;

&lt;p&gt;Three things surprise people here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keys cannot be read back.&lt;/strong&gt; The card will tell you whether a key is correct, and nothing else. A dump therefore never contains the keys in the sense of revealing them — the tool that produced the dump either already knew them or the sector is showing its factory defaults.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The access bits are stored three times.&lt;/strong&gt; The four bytes hold the permission bits plus two inverted copies, so that a single flipped bit can be detected rather than obeyed. That is why the same configuration shows up as &lt;code&gt;FF 07 80&lt;/code&gt; in almost every tutorial: it is not a magic constant, it is one particular set of permissions written into all three copies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A factory-fresh card is recognisable.&lt;/strong&gt; When a sector has never been configured, its trailer usually still reads &lt;code&gt;FF FF FF FF FF FF FF 07 80 69 FF FF FF FF FF FF&lt;/code&gt;. If you see that in a sector you expected to be in use, the card has not been personalised — or somebody reset it, which for a credential is worse news than a wrong number.&lt;/p&gt;

&lt;h1&gt;
  
  
  The 4K cards break the pattern
&lt;/h1&gt;

&lt;p&gt;A 4K card is not "1K times four". It holds 4096 bytes in &lt;strong&gt;40 sectors&lt;/strong&gt;: sectors 0 to 31 are four blocks each, exactly like the 1K layout, and sectors 32 to 39 are &lt;strong&gt;sixteen blocks each&lt;/strong&gt;. Block numbering continues past 63 instead of stopping there, and the trailer of a 16-block sector is block 15 of that sector, not block 3.&lt;/p&gt;

&lt;p&gt;Any code that assumes "16 sectors" or "the trailer is at index 3" works perfectly on the card it was tested with and produces quietly wrong results on the other one. That is the same failure shape as reading a 34-bit credential with a 26-bit parser: no error, just a different and entirely plausible answer.&lt;/p&gt;

&lt;h1&gt;
  
  
  Three readings to distrust
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A number that looks clean.&lt;/strong&gt; If a UID reads as a round decimal or a familiar pattern, check the byte order before believing it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A dump shorter than you expected.&lt;/strong&gt; Truncated dumps are common; a 1K card is 1024 bytes and a 4K card is 4096, so count before analysing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A sector with no trailer.&lt;/strong&gt; If your dump has 15 bytes in a block, the tool dropped one and every offset after it has shifted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To settle the first of those three, &lt;a href="https://toolstead.net/card-number-converter/" rel="noopener noreferrer"&gt;flip the byte order and compare&lt;/a&gt;: paste the UID in whichever reading your dump uses and the page prints both, each labelled with how it was read, so a reversed match is visible instead of inferred.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where this connects to the wire
&lt;/h1&gt;

&lt;p&gt;The dump is the card side; the &lt;a href="https://toolstead.net/blog/wiegand-26-vs-34/" rel="noopener noreferrer"&gt;Wiegand frame&lt;/a&gt; is the wire side, and the two disagree in ways that matter. A panel often stores only the low bytes of a UID, or converts it through a facility-code layout that was designed for a different card generation. When a card "works everywhere except one door", the useful comparison is dump bytes against frame bits — not the numbers printed on two different screens.&lt;/p&gt;

&lt;p&gt;If you also have a file whose type you are inferring from its first bytes, &lt;a href="https://toolstead.net/blog/image-file-signatures/" rel="noopener noreferrer"&gt;reading signatures&lt;/a&gt; covers the same discipline for a different kind of binary: the first bytes are evidence, and the extension is not.&lt;/p&gt;

</description>
      <category>hardware</category>
      <category>security</category>
      <category>programming</category>
      <category>embedded</category>
    </item>
    <item>
      <title>Image File Signatures: PNG, JPEG, WebP, AVIF and More</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Wed, 11 Nov 2026 01:43:07 +0000</pubDate>
      <link>https://dev.to/toolstead/image-file-signatures-png-jpeg-webp-avif-and-more-4d9b</link>
      <guid>https://dev.to/toolstead/image-file-signatures-png-jpeg-webp-avif-and-more-4d9b</guid>
      <description>&lt;p&gt;Give a decoder three things — a filename ending in &lt;code&gt;.png&lt;/code&gt;, a header saying &lt;code&gt;image/jpeg&lt;/code&gt;, and a byte stream that starts with &lt;code&gt;FF D8 FF&lt;/code&gt; — and only one of them is worth trusting. The extension is a naming convention that survives any rename. The declared MIME type is a claim written by whoever produced the data, and it is wrong often enough that treating it as fact is how you end up with a file that opens in your browser and breaks in every other tool.&lt;/p&gt;

&lt;p&gt;The byte stream is the only part that cannot lie, because those bytes are what a decoder has to draw.&lt;/p&gt;

&lt;h1&gt;
  
  
  What a signature actually is
&lt;/h1&gt;

&lt;p&gt;Every container format begins with a fixed pattern so that readers can identify it without parsing the whole file. The pattern is at a known offset — usually byte 0, sometimes byte 4 — and it is short: two to twelve bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PNG    89 50 4E 47 0D 0A 1A 0A     then an IHDR chunk
JPEG   FF D8 FF                     then a segment marker
GIF    47 49 46 38   ("GIF8")       then 39 or 37 for version
WebP   52 49 46 46 __ __ __ __ 57 45 42 50    ("RIFF" size "WEBP")
BMP    42 4D         ("BM")
TIFF   49 49 2A 00 (little-endian) / 4D 4D 00 2A (big-endian)
ICO    00 00 01 00
AVIF   66 74 79 70 at offset 4, major brand "avif"
HEIC   66 74 79 70 at offset 4, major brand "heic" or "mif1"
JP2    00 00 00 0C 6A 50 20 20 0D 0A 87 0A
PSD    38 42 50 53  ("8BPS")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details in that list explain most of the confusion people run into.&lt;/p&gt;

&lt;p&gt;The first is that PNG's signature contains &lt;code&gt;0D 0A&lt;/code&gt; — a CRLF — and &lt;code&gt;1A&lt;/code&gt;, a DOS end-of-file marker. Both are there deliberately: they were chosen in the 1990s so that a PNG file transferred through a text-mode FTP connection would be visibly corrupted rather than silently mangled. It is also why a PNG's first bytes look like text control characters in a hex dump.&lt;/p&gt;

&lt;p&gt;The second is that AVIF, HEIC and HEIF &lt;strong&gt;share the same container&lt;/strong&gt;. They are all ISO base media files, so all of them begin with an &lt;code&gt;ftyp&lt;/code&gt; box; what distinguishes them is the four-character brand at offset 8. A decoder that only checks for &lt;code&gt;ftyp&lt;/code&gt; will happily report "HEIC" for an AVIF file. This is why a correct implementation returns a &lt;em&gt;ranked list&lt;/em&gt; of candidates rather than one answer, and why our &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; tool reports the evidence (&lt;code&gt;ftyp box + major brand "avif"&lt;/code&gt;) instead of just a format name.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reading the bytes in JavaScript
&lt;/h1&gt;

&lt;p&gt;Four lines, no dependencies. Take the first twelve bytes after decoding and compare them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ascii&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x4e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x47&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/png'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xd8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/jpeg'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x49&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x38&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/gif'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x52&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x49&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x46&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;ascii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'WEBP'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/webp'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ascii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'ftyp'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ascii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'avif'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'avis'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/avif'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'heic'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'heix'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'mif1'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'image/heic'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note what happens after the signature check: &lt;code&gt;head&lt;/code&gt; is only the first twelve bytes, so this identifies the container, not the contents. A signature says "this is a PNG stream", never "this PNG is complete". Those are different questions, and conflating them is behind a large share of "the file looks fine but the image is half grey" reports — JPEG in particular has no total-length field in its header, so a truncated JPEG still looks like a perfectly legal JPEG for the first few kilobytes.&lt;/p&gt;

&lt;h1&gt;
  
  
  The four impostors you will actually meet
&lt;/h1&gt;

&lt;p&gt;When a decoder reports "not an image", the more useful question is &lt;em&gt;what is it&lt;/em&gt;. In practice, four families account for almost everything that arrives where a picture was expected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compressed archives.&lt;/strong&gt; &lt;code&gt;1F 8B&lt;/code&gt; is gzip, &lt;code&gt;78 9C&lt;/code&gt; is a zlib stream, &lt;code&gt;50 4B 03 04&lt;/code&gt; is a ZIP entry, &lt;code&gt;50 4B 05 06&lt;/code&gt; is an empty ZIP's end-of-central-directory record. Gzipped JSON that someone Base64-encoded to move it through a text channel lands here, and it is by far the most common cause of this report.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documents.&lt;/strong&gt; &lt;code&gt;25 50 44 46&lt;/code&gt; is &lt;code&gt;%PDF&lt;/code&gt;. A PDF that a browser previews inline is easy to mistake for an image when you only see a thumbnail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Executables.&lt;/strong&gt; &lt;code&gt;7F 45 4C 46&lt;/code&gt; is ELF, &lt;code&gt;00 61 73 6D&lt;/code&gt; is a WebAssembly module, &lt;code&gt;FF FE&lt;/code&gt; or &lt;code&gt;FE FF&lt;/code&gt; at offset 0 indicates UTF-16 text.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text that was already encoded.&lt;/strong&gt; A string of &lt;code&gt;0-9a-f&lt;/code&gt; is hexadecimal, not Base64 with unusual characters. Decoding hex bytes as Base64 produces plausible-looking garbage instead of an error, which is why the distinction matters: the fix is to parse it as hex, not to keep re-encoding it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Telling someone "it decoded to 200 KB but the bytes are gzip" is a different, and much more useful, statement than "invalid input".&lt;/p&gt;

&lt;h1&gt;
  
  
  Why a signature check belongs before the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element
&lt;/h1&gt;

&lt;p&gt;If you assign decoded bytes to an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; and the format is wrong, the browser fails silently: a broken-image icon, an &lt;code&gt;error&lt;/code&gt; event, and nothing in the console. No exception, no diagnostic. A signature check converts that silence into a sentence, and it costs microseconds because it reads twelve bytes.&lt;/p&gt;

&lt;p&gt;To do it by hand on a file you already have, &lt;a href="https://toolstead.net/file-signature-checker/" rel="noopener noreferrer"&gt;drop the file and read its first bytes&lt;/a&gt; — the page prints the signature, the type the name claims and whether the two agree, and the file never leaves the browser.&lt;/p&gt;

&lt;p&gt;The same check is what makes it safe to accept raw strings with no &lt;code&gt;data:&lt;/code&gt; prefix at all — see &lt;a href="https://toolstead.net/base64-without-prefix/" rel="noopener noreferrer"&gt;the raw string decoder&lt;/a&gt; — because the format can be established from the bytes rather than from a header that may not exist.&lt;/p&gt;

&lt;p&gt;It is also the check that turns a silent failure into a sentence. When a payload decodes but the format is wrong, the useful output is not "invalid image" but "these bytes are gzip"; &lt;a href="https://toolstead.net/blog/base64-image-broken-fix/" rel="noopener noreferrer"&gt;Base64 image not showing&lt;/a&gt; walks through that decision order, and &lt;a href="https://toolstead.net/blog/decode-base64-image-python-node-browser/" rel="noopener noreferrer"&gt;decoding in Python, Node and the browser&lt;/a&gt; covers the runtime differences you hit while getting the bytes in the first place.&lt;/p&gt;

&lt;p&gt;One caveat about offsets: they are byte offsets into the &lt;strong&gt;decoded&lt;/strong&gt; stream, and Base64 decoding has to be finished first. Whitespace, line breaks and padding in the encoded form do not shift anything after decoding, which is exactly why stripping them first is safe. If you try to reason about signatures on the Base64 text itself, you get confused quickly: three bytes become four characters, so a PNG's eight-byte signature occupies about eleven characters in encoded form, and their values are unrelated to &lt;code&gt;89 50 4E 47&lt;/code&gt;. Decode first, then look.&lt;/p&gt;

&lt;h1&gt;
  
  
  Signatures are evidence, not guarantees
&lt;/h1&gt;

&lt;p&gt;None of this proves a file is well-formed. A signature check answers "which decoder should I hand this to", and a decoder's own validation answers the rest. That is the right division of labour: the signature is the cheap, reliable first question, and the format's own integrity rules — chunk lengths and a terminating &lt;code&gt;IEND&lt;/code&gt; for PNG, the marker chain for JPEG — do the expensive work afterwards.&lt;/p&gt;

&lt;p&gt;It also explains why "force this format" is a legitimate escape hatch for the rare file whose signature is wrong but whose contents are fine: real-world data is occasionally mislabelled at the source, and the bytes should be given the last word. Just be aware that you are overriding the only part of the input that was not a claim, so verify the result before shipping it.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>security</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Base64 Overhead: Why Data URIs Make Files 33% Bigger</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Tue, 10 Nov 2026 01:48:13 +0000</pubDate>
      <link>https://dev.to/toolstead/base64-overhead-why-data-uris-make-files-33-bigger-406c</link>
      <guid>https://dev.to/toolstead/base64-overhead-why-data-uris-make-files-33-bigger-406c</guid>
      <description>&lt;p&gt;Base64 makes data bigger. Not slightly bigger — a third bigger, every time, with no exceptions and no settings to tune. The number is not a quirk of any particular implementation; it falls directly out of the encoding, and understanding where it comes from makes the inlining decision much easier.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where the third comes from
&lt;/h1&gt;

&lt;p&gt;Base64 exists to represent arbitrary bytes using only characters that survive text transport. The alphabet has 64 symbols, which is 6 bits of information per character. Bytes are 8 bits. So the encoder reads 3 bytes (24 bits) at a time and writes 4 characters (4 × 6 = 24 bits).&lt;/p&gt;

&lt;p&gt;Three bytes in, four characters out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 bytes → 4 chars        4 / 3 = 1.333...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the whole story. A 300,000-byte image becomes a 400,000-character string. The overhead is exactly 33.3%, and it applies to the payload only — the &lt;code&gt;data:image/png;base64,&lt;/code&gt; prefix adds a further 22 characters, which matters for tiny assets and is irrelevant for large ones.&lt;/p&gt;

&lt;p&gt;The one escape hatch is padding. If the input length is not a multiple of three, the last group is padded with &lt;code&gt;=&lt;/code&gt; characters that carry no information, so the exact size is &lt;code&gt;4 × ceil(n / 3)&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 byte&lt;/strong&gt; → 4 characters, &lt;strong&gt;+300%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3 bytes&lt;/strong&gt; → 4 characters, &lt;strong&gt;+33%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;100 bytes&lt;/strong&gt; → 136 characters, &lt;strong&gt;+36%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1,000 bytes&lt;/strong&gt; → 1,336 characters, &lt;strong&gt;+33.6%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1,000,000 bytes&lt;/strong&gt; → 1,333,336 characters, &lt;strong&gt;+33.3%&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The small-input entries are the argument against inlining tiny assets for size reasons: a 1-byte file becomes 4 characters plus a 22-character prefix, and the prefix dominates.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why gzip does not save you
&lt;/h1&gt;

&lt;p&gt;On text, gzip routinely achieves 60–80% reduction, which is why the 33% figure is sometimes dismissed as irrelevant. That reasoning holds for JSON and HTML and fails completely for images.&lt;/p&gt;

&lt;p&gt;Compression works by finding redundancy. JPEG, PNG, WebP and AVIF have already removed almost all of it — that is what those formats do — so their bytes are close to random from a compressor's point of view. Base64 encodes those incompressible bytes into a restricted 64-symbol alphabet, which adds no redundancy a compressor can exploit. The result is the worst of both worlds: the data is 33% larger and still does not compress.&lt;/p&gt;

&lt;p&gt;You can measure this in one command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# a real image, already compressed&lt;/span&gt;
base64 -w0 photo.jpg &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; photo.b64
ls -l photo.jpg photo.b64
gzip -9 -c photo.b64 &lt;span class="o"&gt;|&lt;/span&gt; wc -c    &lt;span class="c"&gt;# barely smaller than photo.b64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The garbled case is different. If you Base64-encode a &lt;em&gt;text&lt;/em&gt; payload — JSON, XML, a log — gzip will compress the encoded form well, sometimes better than the original, because Base64 of text still contains recognisable structure. So "does gzip fix the 33%" has two answers, and they depend entirely on whether the payload was compressible to begin with.&lt;/p&gt;

&lt;h1&gt;
  
  
  The cost is not only bytes
&lt;/h1&gt;

&lt;p&gt;Inflating a stylesheet or an HTML document has three costs that do not show up in a file-size comparison:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Render blocking.&lt;/strong&gt; A data URI inside a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; block or a linked CSS file is part of that file. The browser cannot paint until it has parsed it, and it cannot parse it until it has downloaded it. An external image request is not on that critical path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No independent caching.&lt;/strong&gt; Change one byte of a stylesheet that inlines twenty icons and every visitor re-downloads all twenty. As separate files they would each have their own cache entry and their own validator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parsing and memory.&lt;/strong&gt; The bytes must be decoded and held as a string before they become an image. A 400 KB string in a stylesheet is 400 KB of text the parser must walk, and it stays in the CSSOM.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of these are fatal. All of them are invisible if you only compare file sizes.&lt;/p&gt;

&lt;p&gt;Two of those three costs come from how the string is embedded rather than from Base64 itself, which is worth separating: &lt;a href="https://toolstead.net/blog/what-is-base64-data-uri/" rel="noopener noreferrer"&gt;what a data URI actually is&lt;/a&gt; walks the grammar and the four ways one fails to render, and most of them are wrapping problems rather than encoding problems.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where the break-even actually sits
&lt;/h1&gt;

&lt;p&gt;There is no universal threshold, but the decision is narrow enough to state as rules of thumb:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Under ~2 KB, inline it.&lt;/strong&gt; The bytes saved by avoiding a request (headers alone are often 300–800 bytes over HTTP/1.1, less over HTTP/2) can exceed the 33%. A single-colour icon or a tiny SVG is the classic case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2–10 KB, it depends on caching.&lt;/strong&gt; A favicon or a logo that never changes can be inlined safely; something that ships weekly should not be, and an asset used on ten pages should be a file so it is downloaded once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over ~10 KB, use a file.&lt;/strong&gt; The overhead is now measured in kilobytes, there is no request to save, and you have given up caching for nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over ~1 MB, never inline it in a document.&lt;/strong&gt; Beyond the size penalty you are now in territory where some tools truncate long attributes and some parsers get slow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For images specifically, there is a better question than "how big is the Base64 string": &lt;em&gt;could this be an SVG?&lt;/em&gt; A vector logo is a few hundred bytes of path data, encodes to under a kilobyte, and is resolution-independent. Inlining that is a clear win, and it is the case where the technique earns its reputation.&lt;/p&gt;

&lt;p&gt;The table above covers round numbers. For a real file, &lt;a href="https://toolstead.net/image-to-base64/" rel="noopener noreferrer"&gt;convert an image and watch the payload grow&lt;/a&gt; — the page reports the exact character count, the byte count and the overhead that falls out of the two, in the Data URI, plain and CSS forms.&lt;/p&gt;

&lt;h1&gt;
  
  
  Measuring it on your own site
&lt;/h1&gt;

&lt;p&gt;Two numbers tell you whether inlining is helping: the transfer size of the document that contains the data URI, and the transfer size of the equivalent page with external assets. If the inlined version is 30% larger, it is 30% larger — the 33% is not recoverable by the network layer.&lt;/p&gt;

&lt;p&gt;The measurement is easy to get wrong in one specific way: comparing the &lt;em&gt;uncompressed&lt;/em&gt; size of the image with the &lt;em&gt;compressed&lt;/em&gt; size of the document. Compare like with like, and remember that the data URI's bytes travel inside a response that gzip can only partially shrink. Our &lt;a href="https://toolstead.net/png-to-base64/" rel="noopener noreferrer"&gt;PNG to Base64&lt;/a&gt; and &lt;a href="https://toolstead.net/jpg-to-base64/" rel="noopener noreferrer"&gt;JPG to Base64&lt;/a&gt; pages report the output length and the overhead as you convert, which is usually enough to see immediately that a 900 KB photograph should not be an inline background image.&lt;/p&gt;

&lt;p&gt;For the cases where you do want the string — a small icon in critical CSS, an email template that cannot reference external files, a single-file HTML export, a JSON fixture in a test — the overhead is a fair price for self-containment, and knowing the exact number keeps it a deliberate choice rather than an accident. If you are deciding between the two ways of holding an image in memory rather than in a document, &lt;a href="https://toolstead.net/blog/data-uri-vs-blob-url/" rel="noopener noreferrer"&gt;data URI vs blob URL&lt;/a&gt; covers the other half of this trade.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Decode a Base64 Image in Python, Node.js and the Browser</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Mon, 09 Nov 2026 02:05:32 +0000</pubDate>
      <link>https://dev.to/toolstead/decode-a-base64-image-in-python-nodejs-and-the-browser-3kei</link>
      <guid>https://dev.to/toolstead/decode-a-base64-image-in-python-nodejs-and-the-browser-3kei</guid>
      <description>&lt;p&gt;Base64 is standardised, but decoders are not standardised in how forgiving they are. The same string that Python accepts can be silently mangled by Node, and the same string that Node happily decodes will make &lt;code&gt;atob&lt;/code&gt; throw. Knowing which runtime does what turns a confusing failure into an expected one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Python: strict, explicit, and the best choice for validation
&lt;/h1&gt;

&lt;p&gt;Python's &lt;code&gt;base64&lt;/code&gt; module is the most honest of the three. By default it ignores newlines and other characters it does not recognise in the middle of the string; with &lt;code&gt;validate=True&lt;/code&gt; it rejects anything outside the alphabet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;binascii&lt;/span&gt;

&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s1"&gt;','&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;
&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'-'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'+'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# base64url → standard&lt;/span&gt;
&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s1"&gt;'='&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                        &lt;span class="c1"&gt;# restore dropped padding&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;binascii&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;SystemExit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;'not valid base64: {e}'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'out.png'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'wb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things in that snippet are worth pointing out. The padding repair happens &lt;em&gt;before&lt;/em&gt; validation, so a string that lost its &lt;code&gt;=&lt;/code&gt; in transit is accepted. The alphabet conversion happens before padding repair, because the padding calculation uses the length after substitution — the two are the same length, so the order is safe, but doing it the other way round in a language with different assumptions is a common source of off-by-one bugs. And &lt;code&gt;binascii.Error&lt;/code&gt; is the exception to catch; &lt;code&gt;base64.b64decode&lt;/code&gt; does not raise &lt;code&gt;ValueError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want the strictest possible check, decode and then verify the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="no"&gt;PIL&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BytesIO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# raises on a truncated or structurally broken file&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;verify()&lt;/code&gt; is the closest thing to answering "is this actually a complete image", which is a different question from "did the Base64 decode". A truncated JPEG decodes perfectly and fails here.&lt;/p&gt;

&lt;h1&gt;
  
  
  Node.js: lenient to the point of being dangerous
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Buffer.from(s, 'base64')&lt;/code&gt; does not throw on invalid input. It discards characters it does not recognise and returns whatever it could decode, which means a truncated or polluted string produces bytes with no error at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hex'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is convenient for forgiving input and terrible for validation, because a string that lost the last 20% of its characters still returns a buffer. If you are decoding data you did not produce, validate the shape before decoding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="n"&gt;r&lt;/span&gt;\&lt;span class="n"&gt;n&lt;/span&gt;\&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'truncated: length % 4 === 1'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Za&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;z0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;+/=&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'illegal character in payload'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node also handles base64url natively, which avoids the manual substitution entirely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64url'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// '-' and '_' understood, padding optional&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For files on disk, the whole thing is a one-liner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;writeFileSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'node:fs'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'out.png'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The browser: &lt;code&gt;atob&lt;/code&gt;, bytes, and one crucial detail
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;atob&lt;/code&gt; decodes to a "binary string" — one character per byte — which is not the same thing as bytes. Passing those characters anywhere that expects text corrupts everything above &lt;code&gt;0x7F&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// real bytes&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'image/png'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Uint8Array.from&lt;/code&gt; step is not optional. &lt;code&gt;new Blob([atob(payload)])&lt;/code&gt; looks equivalent and produces a corrupt file for any image containing a byte above 127 — which is most of them, since compressed data is close to uniformly random. The symptom is an image that "almost" works: correct dimensions, green or grey bands, or a decoder rejecting it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;atob&lt;/code&gt; is also the strictest of the three about whitespace. Per the current specification it throws &lt;code&gt;InvalidCharacterError&lt;/code&gt; on a newline, so a MIME-wrapped string must be stripped first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="n"&gt;r&lt;/span&gt;\&lt;span class="n"&gt;n&lt;/span&gt;\&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Command line, for when there is no script
&lt;/h1&gt;

&lt;p&gt;Two tools do this and they are not identical in strictness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s'&lt;/span&gt; &lt;span class="s2"&gt;"$B64"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sed &lt;span class="s1"&gt;'s/^.*,//'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s'&lt;/span&gt; &lt;span class="s2"&gt;"$payload"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; base64 -d &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; out.bin        &lt;span class="c"&gt;# GNU coreutils, stops on bad input&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s'&lt;/span&gt; &lt;span class="s2"&gt;"$payload"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; openssl base64 -d -A &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; out.bin   &lt;span class="c"&gt;# -A ignores newlines&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;base64 -d&lt;/code&gt; on GNU systems reports &lt;code&gt;invalid input&lt;/code&gt; and exits non-zero on a character outside the alphabet, which makes it a decent validator. macOS uses BSD &lt;code&gt;base64&lt;/code&gt; with &lt;code&gt;-D&lt;/code&gt; instead of &lt;code&gt;-d&lt;/code&gt;. Follow either with a signature check, which is the only way to know what you got:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;file out.bin          &lt;span class="c"&gt;# → PNG image data, 1 x 1, 8-bit/color RGBA, non-interlaced&lt;/span&gt;
xxd out.bin &lt;span class="o"&gt;|&lt;/span&gt; head -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  A fifth way in the browser: let &lt;code&gt;fetch&lt;/code&gt; do it
&lt;/h1&gt;

&lt;p&gt;There is an approach that skips &lt;code&gt;atob&lt;/code&gt; entirely and avoids every size limit discussed above, because the browser decodes the data URI internally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// uri is the full data: string&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;arrayBuffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// if you need the bytes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; understands &lt;code&gt;data:&lt;/code&gt; URLs as a scheme, so this works with no network request. It returns a &lt;code&gt;Blob&lt;/code&gt; directly, which pairs naturally with a blob URL and with &lt;code&gt;arrayBuffer()&lt;/code&gt; for byte inspection, and it does not build an intermediate binary string — so the double-memory problem of &lt;code&gt;atob&lt;/code&gt; plus &lt;code&gt;Uint8Array&lt;/code&gt; does not arise.&lt;/p&gt;

&lt;p&gt;Two caveats. A strict &lt;code&gt;Content-Security-Policy&lt;/code&gt; can block this: the fetch is governed by &lt;code&gt;connect-src&lt;/code&gt;, so a policy that does not allow &lt;code&gt;data:&lt;/code&gt; there will fail the request, and the error message talks about the connection rather than about Base64. And &lt;code&gt;fetch&lt;/code&gt; is asynchronous, which is usually an improvement but does change the shape of surrounding code.&lt;/p&gt;

&lt;p&gt;For a converter that decodes on every keystroke it is the simplest of the five approaches; for a script that needs to validate and repair the input first, doing the normalisation yourself and then calling one strict decoder remains clearer.&lt;/p&gt;

&lt;h1&gt;
  
  
  A behaviour comparison worth remembering
&lt;/h1&gt;

&lt;p&gt;Same input, three runtimes, three outcomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Contains &lt;code&gt;\n&lt;/code&gt;&lt;/strong&gt; — Python: ignored by default · Node: ignored · browser &lt;code&gt;atob&lt;/code&gt;: &lt;strong&gt;throws&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contains spaces&lt;/strong&gt; — Python: ignored by default · Node: ignored · browser &lt;code&gt;atob&lt;/code&gt;: &lt;strong&gt;throws&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contains &lt;code&gt;%3D&lt;/code&gt;&lt;/strong&gt; — Python: error with &lt;code&gt;validate=True&lt;/code&gt; · Node: &lt;code&gt;%&lt;/code&gt; skipped · browser &lt;code&gt;atob&lt;/code&gt;: &lt;strong&gt;throws&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Missing padding&lt;/strong&gt; — Python: error · Node: decodes · browser &lt;code&gt;atob&lt;/code&gt;: &lt;strong&gt;throws&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Length % 4 == 1&lt;/strong&gt; — Python: error · Node: decodes (wrong bytes) · browser &lt;code&gt;atob&lt;/code&gt;: throws&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contains &lt;code&gt;-&lt;/code&gt; or &lt;code&gt;_&lt;/code&gt;&lt;/strong&gt; — Python: error (&lt;code&gt;urlsafe&lt;/code&gt; variant needed) · Node: &lt;code&gt;'base64url'&lt;/code&gt; handles it · browser &lt;code&gt;atob&lt;/code&gt;: &lt;strong&gt;throws&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pattern is that Node never tells you anything and Python tells you if you ask. Neither is wrong for its intended use — Node optimises for tolerant ingestion of real-world data, Python for correctness by default. The practical advice follows directly: &lt;strong&gt;decode with the strictest runtime you have, and do the normalisation yourself.&lt;/strong&gt; If you repair the input explicitly, record what you repaired, and verify the output bytes afterwards, the three runtimes agree — and you find out about a truncated string instead of rendering half an image.&lt;/p&gt;

&lt;p&gt;That last step, verifying the decoded bytes, is the one people skip. Two checks cover most of it: the first bytes should match a known format signature (see &lt;a href="https://toolstead.net/blog/image-file-signatures/" rel="noopener noreferrer"&gt;image file signatures&lt;/a&gt;), and the length should match what the producer reported. If the length is short, the string was truncated in transit — the arithmetic behind that check is in &lt;a href="https://toolstead.net/blog/base64-line-breaks-padding/" rel="noopener noreferrer"&gt;line breaks and padding&lt;/a&gt;. If you would rather not write it, &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; performs the whole sequence — normalise, repair, decode, identify, verify — and reports each step it took, including the ones it decided not to take, which is often the more useful information.&lt;/p&gt;

&lt;p&gt;The reverse direction has its own traps, and they are not symmetric. Encoding accepts bytes without complaint, so a wrong header or a missing prefix only surfaces later, at the consumer. &lt;a href="https://toolstead.net/blog/image-to-base64-javascript/" rel="noopener noreferrer"&gt;Image to Base64 in JavaScript&lt;/a&gt; covers that side, including what each browser appends to the string without being asked.&lt;/p&gt;

</description>
      <category>python</category>
      <category>node</category>
      <category>javascript</category>
      <category>base64</category>
    </item>
    <item>
      <title>Data URI vs Blob URL: Which Should You Use for Images?</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Sun, 08 Nov 2026 01:35:33 +0000</pubDate>
      <link>https://dev.to/toolstead/data-uri-vs-blob-url-which-should-you-use-for-images-2m9b</link>
      <guid>https://dev.to/toolstead/data-uri-vs-blob-url-which-should-you-use-for-images-2m9b</guid>
      <description>&lt;p&gt;When you have image bytes in memory and want to display them, there are two ways to get them into an &lt;code&gt;img&lt;/code&gt; element without a server round trip. They are frequently treated as interchangeable, and they are not: one is a portable but expensive text encoding, the other is a cheap reference that only exists while the page does.&lt;/p&gt;

&lt;h1&gt;
  
  
  The short version
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data URI&lt;/strong&gt; — &lt;code&gt;data:image/png;base64,iVBOR...&lt;/code&gt; — the bytes encoded as text, embedded in the URL itself. Self-contained, portable, serialisable, ~33% larger, and it lives in the DOM as a string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blob URL&lt;/strong&gt; — &lt;code&gt;blob:https://example.com/5f2a...&lt;/code&gt; — a reference to a &lt;code&gt;Blob&lt;/code&gt; the browser holds in memory. No encoding, no size penalty, but it exists only in the document that created it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// data URI: the bytes are in the string&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'data:image/png;base64,'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;base64Payload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// blob URL: the bytes stay in a Blob, the URL is just a handle&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// later, when the image is gone:&lt;/span&gt;
&lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;revokeObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What each one costs
&lt;/h1&gt;

&lt;p&gt;The data URI's 33% is structural, not an implementation detail: Base64 packs three bytes into four characters, and image bytes are already compressed, so gzip does not win them back. A 300 KB photograph becomes a 400 KB string, and inside JavaScript that string is stored as UTF-16 — two bytes per character — so it occupies about 800 KB in memory, on top of the original bytes you still hold.&lt;/p&gt;

&lt;p&gt;The blob URL's cost is essentially zero. The browser keeps the bytes once, in the &lt;code&gt;Blob&lt;/code&gt; you already created, and the URL is a short string that points at them. There is no encode step, no decode step, and no second copy.&lt;/p&gt;

&lt;p&gt;That difference decides most cases on its own. If the payload is large or transient — a preview of a file the user just selected, a generated image, a decoded Base64 payload — a blob URL is the right answer.&lt;/p&gt;

&lt;h1&gt;
  
  
  What each one can do
&lt;/h1&gt;

&lt;p&gt;The trade-off runs the other way for portability, and this is where the choice stops being about size.&lt;/p&gt;

&lt;p&gt;A data URI is a &lt;strong&gt;value&lt;/strong&gt;. You can put it in a JSON response, write it into an HTML file, store it in &lt;code&gt;localStorage&lt;/code&gt;, paste it into an email, embed it in CSS, or send it to a colleague in a chat message. It survives serialisation, page reloads and process restarts, because everything needed is in the string.&lt;/p&gt;

&lt;p&gt;A blob URL is a &lt;strong&gt;handle&lt;/strong&gt;. It is scoped to the document that created it: open the same URL in a new tab and it means nothing; save it to a file and it is a dead reference; reload the page and every blob URL the previous document created becomes invalid. It also cannot be used across origins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'blob:https://example.com/5f2a...'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// serialises fine&lt;/span&gt;
&lt;span class="c1"&gt;// ...and means nothing to whoever reads it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The memory leak nobody notices
&lt;/h1&gt;

&lt;p&gt;Blob URLs must be revoked. Every &lt;code&gt;createObjectURL&lt;/code&gt; call pins a &lt;code&gt;Blob&lt;/code&gt; in memory until either the page is destroyed or you call &lt;code&gt;revokeObjectURL&lt;/code&gt;. In a preview that updates on every keystroke — exactly the pattern this site's decoder uses — forgetting to revoke means you accumulate one pinned image per paste:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;currentUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;revokeObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// release the previous one&lt;/span&gt;
  &lt;span class="n"&gt;currentUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;currentUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Revoking a URL that an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; has already loaded is safe and correct: the image keeps its decoded pixels, and the underlying bytes are released. Revoking &lt;em&gt;while&lt;/em&gt; an image is still loading cancels the load in some browsers, so revoke the old URL when you replace it, not the new one you just assigned.&lt;/p&gt;

&lt;p&gt;The same leak exists for data URIs, but in a less visible form: a long string held in a DOM attribute is memory that only goes away when the element does. A 400 KB string set as &lt;code&gt;img.src&lt;/code&gt; and then replaced leaves the browser free to release it, but the string in your own variable does not go anywhere until you drop the reference.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where each one wins in practice
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Use a blob URL when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;previewing a file or a decoded payload, especially more than a few hundred kilobytes;&lt;/li&gt;
&lt;li&gt;the image is generated (canvas, &lt;code&gt;createImageBitmap&lt;/code&gt;, a decoded Base64 payload) and you control its lifetime;&lt;/li&gt;
&lt;li&gt;you are downloading it — &lt;code&gt;URL.createObjectURL&lt;/code&gt; plus the &lt;code&gt;download&lt;/code&gt; attribute is the standard way to save bytes without a server;&lt;/li&gt;
&lt;li&gt;the same bytes are shown more than once, since the URL is reusable and the data URI would be duplicated in each attribute.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use a data URI when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the value has to survive outside the current document — JSON, &lt;code&gt;localStorage&lt;/code&gt;, an email template, an HTML file;&lt;/li&gt;
&lt;li&gt;you are embedding a small asset in CSS or an HTML document where a second request would cost more than the bytes;&lt;/li&gt;
&lt;li&gt;you are producing something a human will copy and paste, which is the entire premise of a Base64 converter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Neither, when:&lt;/strong&gt; you already have a real URL. An object or file served over HTTP is cached, streamable, and costs the browser nothing to display. Encoding bytes to Base64 to avoid a request is a trade that only pays off for small assets — see &lt;a href="https://toolstead.net/blog/base64-overhead-33-percent/" rel="noopener noreferrer"&gt;why Base64 makes your page 33% bigger&lt;/a&gt; for where that line falls.&lt;/p&gt;

&lt;p&gt;There is one more case worth naming, because it is the one people reach for a data URI to solve: getting the string into a place that only accepts text — a JSON field, a config file, an email template. Neither a blob URL nor a data URI is the right answer there if the image is large; a real URL plus a text reference is. And if you are producing that string by hand rather than in code, &lt;a href="https://toolstead.net/image-to-base64/" rel="noopener noreferrer"&gt;Image to Base64&lt;/a&gt; exists precisely to emit the right wrapper for the consumer you have in mind.&lt;/p&gt;

&lt;p&gt;A note on decoding the other direction, since it comes up in the same code paths: if what you hold is a Base64 string rather than a &lt;code&gt;Blob&lt;/code&gt;, you can go straight to a blob URL without touching &lt;code&gt;atob&lt;/code&gt; — &lt;code&gt;await (await fetch(dataUri)).blob()&lt;/code&gt; does the decoding internally and hands you something &lt;code&gt;createObjectURL&lt;/code&gt; accepts. That is the shortest path from a pasted string to a rendered preview, and it is what this site's decoder does; &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; shows the result while also verifying that the bytes are what the header claimed.&lt;/p&gt;

&lt;p&gt;Either way the starting point is the same: bytes you already hold. When those bytes come from a file the user picked, the conversion step itself — read the file, encode it, attach a header that matches the real format — is in &lt;a href="https://toolstead.net/blog/image-to-base64-javascript/" rel="noopener noreferrer"&gt;Image to Base64 in JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  The one security note worth carrying
&lt;/h1&gt;

&lt;p&gt;Neither scheme executes scripts on its own: an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element does not run scripts inside an SVG, whether the source is a data URI or a blob URL. The risk appears when the resource stops being an image — navigating a top-level document to an &lt;code&gt;image/svg+xml&lt;/code&gt; data URI, or passing user-supplied bytes to something that treats them as markup. Keep the rule narrow and concrete: images go in &lt;code&gt;img&lt;/code&gt;, and a &lt;code&gt;blob:&lt;/code&gt; URL is how you hand the browser bytes you already trust in memory.&lt;/p&gt;

&lt;p&gt;One CSP detail catches people out in both directions. Data URIs need &lt;code&gt;img-src data:&lt;/code&gt; to be allowed, and blob URLs need &lt;code&gt;img-src blob:&lt;/code&gt;. A page with &lt;code&gt;img-src 'self'&lt;/code&gt; blocks both — silently, with only a console violation to explain it. If you add either technique to a site with a strict CSP, add the matching source expression at the same time, or you will spend an afternoon debugging Base64 that was never broken.&lt;/p&gt;

&lt;h1&gt;
  
  
  Questions people actually ask
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;When should I use a data URI instead of a blob URL?&lt;/strong&gt; When the value has to survive outside the current document — a JSON response, &lt;code&gt;localStorage&lt;/code&gt;, an HTML file, an email template, or something a person will copy and paste. A data URI is a value that serialises; a blob URL is a handle that only means something inside the document that created it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why does a &lt;code&gt;blob:&lt;/code&gt; link stop working after a reload?&lt;/strong&gt; Because it is scoped to the document. Reload the page and every blob URL the previous document created becomes invalid; open the same URL in a new tab and it refers to nothing; save it to a file and it is a dead reference. It cannot cross origins either. The bytes only ever existed in that document's memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does each one cost?&lt;/strong&gt; A data URI is about 33% larger, and that is structural — Base64 packs three bytes into four characters, and image bytes are already compressed, so gzip does not win it back. Inside JavaScript the string is stored as UTF-16, so a 300 KB photograph becomes a 400 KB string occupying roughly 800 KB of memory. A blob URL costs essentially nothing: the bytes stay in one &lt;code&gt;Blob&lt;/code&gt; and the URL is a short handle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I have to revoke blob URLs?&lt;/strong&gt; Yes. Every &lt;code&gt;createObjectURL&lt;/code&gt; call pins a &lt;code&gt;Blob&lt;/code&gt; in memory until the page is destroyed or you call &lt;code&gt;revokeObjectURL&lt;/code&gt;. In a preview that updates on every keystroke, forgetting this accumulates one pinned image per paste. Revoke the previous URL when you assign a new one — revoking a URL an image has already loaded is safe, but revoking while it is still loading cancels the load in some browsers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which should I use to preview a large file the user just selected?&lt;/strong&gt; A blob URL. Large or transient payloads are exactly the case where the 33% overhead and the doubled memory of a string in the DOM are pure loss, and you control the lifetime, so the revoke discipline is easy to get right.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>browser</category>
      <category>performance</category>
    </item>
    <item>
      <title>Base64 Line Breaks and Padding: Why They Break Decoders</title>
      <dc:creator>toolstead</dc:creator>
      <pubDate>Sat, 07 Nov 2026 01:42:07 +0000</pubDate>
      <link>https://dev.to/toolstead/base64-line-breaks-and-padding-why-they-break-decoders-5ckh</link>
      <guid>https://dev.to/toolstead/base64-line-breaks-and-padding-why-they-break-decoders-5ckh</guid>
      <description>&lt;p&gt;Two formatting rules cause more Base64 failures than every other cause combined. Both are part of the standard, both are routinely violated in the wild, and strict decoders reject the results — which means the same string can work in one language and fail in another.&lt;/p&gt;

&lt;p&gt;The first rule is that Base64 output is often wrapped across lines. The second is that the tail is padded with &lt;code&gt;=&lt;/code&gt; to round the length up to a multiple of four. Neither changes the data. Both change the string, and decoders disagree about how forgiving to be.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why 76 characters
&lt;/h1&gt;

&lt;p&gt;Base64 predates JSON, HTTP/2 and email attachments. It was designed to survive transport through systems that cared about line length, and the dominant one was email: RFC 2045 wraps encoded bodies at &lt;strong&gt;76 characters&lt;/strong&gt;, a limit chosen to stay under SMTP's 998-octet line maximum while keeping quoted-printable output readable. Every four Base64 characters represent exactly three bytes, so 76 characters is 57 bytes per line — an awkward-looking number that is really just "72 characters plus a little headroom".&lt;/p&gt;

&lt;p&gt;That is why you see this shape in PEM files, MIME bodies and log output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAF
AAH/q842iQAAAABJRU5ErkJggg==
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The newline is not data. It is transport furniture that leaked into the copy-paste layer, and a decoder that treats the string as opaque bytes will stop at the first &lt;code&gt;\n&lt;/code&gt; and report an illegal character at position 76.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why the padding exists, and why the remainder matters
&lt;/h1&gt;

&lt;p&gt;Each group of four Base64 characters encodes three bytes. When the input length is not a multiple of three, the encoder pads the &lt;em&gt;output&lt;/em&gt; with &lt;code&gt;=&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 leftover byte → 2 characters + &lt;code&gt;==&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2 leftover bytes → 3 characters + &lt;code&gt;=&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;0 leftover bytes → no padding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the encoded length is always a multiple of four, once padding is included. Turn that around and you get the single most useful diagnostic rule in this whole area:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After normalisation, a complete Base64 string's length modulo 4 is &lt;strong&gt;0, 2 or 3. A remainder of 1 is impossible.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A remainder of 1 does not mean "slightly malformed". It means characters are missing. There is no 1-byte remainder case in the encoding, so some input byte pair has been cut in half. In practice the cause is almost always truncation: a log line capped at 1024 characters, a terminal that wrapped and lost a line during selection, a database column with a &lt;code&gt;VARCHAR(n)&lt;/code&gt; limit, or a copy that stopped a character early.&lt;/p&gt;

&lt;p&gt;This is worth stating plainly because it is the difference between a fixable problem and an unfixable one. A decoder can repair missing padding — adding &lt;code&gt;=&lt;/code&gt; is deterministic and lossless. It cannot repair a missing character; anything it produces will be a corrupted image, and a tool that "helpfully" pads and decodes a remainder-of-1 string is handing you a picture that is wrong in a way you cannot see.&lt;/p&gt;

&lt;h1&gt;
  
  
  What different decoders do about it
&lt;/h1&gt;

&lt;p&gt;The behaviour you get depends entirely on which implementation you use, and the differences are not documented prominently anywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt; is strict and explicit. &lt;code&gt;base64.b64decode(s)&lt;/code&gt; ignores newlines but rejects other junk; adding &lt;code&gt;validate=True&lt;/code&gt; makes it reject any character outside the alphabet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;
&lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;b64decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# binascii.Error on bad input&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Node&lt;/strong&gt; is the opposite extreme. &lt;code&gt;Buffer.from(s, 'base64')&lt;/code&gt; silently discards characters it does not recognise. It will decode a string containing spaces, quotes and stray prose without complaint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'iVBO\nRw0K Ggo='&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;   &lt;span class="c1"&gt;// 10 — no error, junk skipped&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That leniency is convenient when you know the input is nearly right, and dangerous when you do not: a truncated string still returns bytes, just fewer of them, and nothing tells you the difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browsers&lt;/strong&gt; sit in the middle. &lt;code&gt;atob&lt;/code&gt; throws &lt;code&gt;InvalidCharacterError&lt;/code&gt; on a newline — but only in strict spec-compliant form; historically it ignored whitespace. Normalising first removes the ambiguity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;\&lt;span class="n"&gt;r&lt;/span&gt;\&lt;span class="n"&gt;n&lt;/span&gt;\&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  A normalisation order that does not lose information
&lt;/h1&gt;

&lt;p&gt;The order matters, because some repairs change what the next check sees. This sequence is what our &lt;a href="https://toolstead.net/base64-to-image/" rel="noopener noreferrer"&gt;Base64 to Image&lt;/a&gt; tool applies, in this order, reporting each step it took:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Strip the &lt;code&gt;data:&lt;/code&gt; prefix if present&lt;/strong&gt; — everything up to the first comma. If there is no comma, there is no prefix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decode percent-encoding&lt;/strong&gt; (&lt;code&gt;%3D&lt;/code&gt; → &lt;code&gt;=&lt;/code&gt;). This happens before the alphabet check, otherwise &lt;code&gt;%&lt;/code&gt; looks like an illegal character.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remove whitespace and line breaks.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strip surrounding quotes&lt;/strong&gt;, which appear when a string is copied out of JavaScript, JSON or a shell script.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unescape literal &lt;code&gt;\n&lt;/code&gt; and &lt;code&gt;\t&lt;/code&gt; sequences&lt;/strong&gt;, which appear when the encoded string was itself stored inside a JSON string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convert the URL-safe alphabet&lt;/strong&gt; (&lt;code&gt;-&lt;/code&gt; → &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;_&lt;/code&gt; → &lt;code&gt;/&lt;/code&gt;) if those characters are present.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix padding&lt;/strong&gt; — add the &lt;code&gt;=&lt;/code&gt; characters the length says are missing, or remove surplus ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the remainder.&lt;/strong&gt; If it is 1, stop and tell the user the data is truncated. Do not decode.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Steps 7 and 8 are the pair that separates a useful decoder from a plausible one. Without step 8 you ship a corrupted image silently; without step 7 you reject every string that lost its padding in transit, which is a large fraction of them.&lt;/p&gt;

&lt;p&gt;Step 1 assumes there is a header to strip. In logs, database columns and message queues the string usually arrives bare, with no &lt;code&gt;data:&lt;/code&gt; at all — nothing to strip, and it is worth &lt;a href="https://toolstead.net/base64-without-prefix/" rel="noopener noreferrer"&gt;decoding the raw string without the prefix&lt;/a&gt; rather than pasting it into a converter that insists on one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deciding when to accept a repaired string
&lt;/h1&gt;

&lt;p&gt;Repair is not the same as validation. Adding padding is safe because it is reversible and determined by the data itself. Stripping whitespace is safe for the same reason. But once you have repaired the &lt;em&gt;form&lt;/em&gt;, the only real check on the &lt;em&gt;content&lt;/em&gt; is structural: does the decoded byte stream look like the thing it is supposed to be?&lt;/p&gt;

&lt;p&gt;That is why the tool reports what it changed rather than quietly fixing things. "Removed whitespace and line breaks (57 positions)" is information: it tells you the string went through a MIME-aware channel. "Added 2 padding characters" tells you the source dropped them — often a URL, a query parameter, or a JavaScript string that was trimmed. Both are normal, and both are worth knowing about when the image that comes out is not the image you expected.&lt;/p&gt;

&lt;p&gt;The one repair that should never be silent is removing characters from the middle of the data. If a decoder has to throw away something other than whitespace to make a string valid, the honest answer is that the input is not Base64 — see &lt;a href="https://toolstead.net/blog/what-is-base64-data-uri/" rel="noopener noreferrer"&gt;what a data URI actually allows&lt;/a&gt; for the header half of this problem, and &lt;a href="https://toolstead.net/blog/base64-image-broken-fix/" rel="noopener noreferrer"&gt;Base64 image not showing&lt;/a&gt; for the full troubleshooting order when you have already decoded something and it still will not display.&lt;/p&gt;

</description>
      <category>base64</category>
      <category>javascript</category>
      <category>debugging</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
