Base64 Decode: Base64 to Text
How do you decode a Base64 string?
Base64 decoding reads those characters back as the bytes they stand for. Base64 exists to carry data through channels that accept only US-ASCII text. It is an encoding, not encryption: any decoder reverses it.
SGk= decodes to "Hi". Base64 decoding reads four characters at a time, turns each back into six bits, and reassembles the 24 bits as three bytes; the equals signs at the end mark a short final group.
- Strip any line breaks. A decoder ignores every character that is not in the alphabet.
- Take the characters 4 at a time and look each one up: its index is a 6-bit number.
- Join the 4 numbers into 24 bits and read them as 3 bytes.
- Drop the bits a = stands for; they carry nothing.
- Read the bytes as UTF-8 text. SGk= becomes Hi.
Base64 to Text Conversion Table
Base64 writes data with 64 characters - A to Z, a to z, 0 to 9, plus (+) and slash (/) - and reserves the equals sign (=) for padding. Each row is one six-bit value and the character that stands for it, so a group's number and its character are the same lookup read in either direction. RFC 4648 s5 swaps the last two rows; the + at index 62 becomes - and the / at index 63 becomes _.
| Index | Six bits | Character |
|---|---|---|
| 0 | 000000 | A |
| 1 | 000001 | B |
| 2 | 000010 | C |
| 3 | 000011 | D |
| 4 | 000100 | E |
| 5 | 000101 | F |
| 6 | 000110 | G |
| 7 | 000111 | H |
| 8 | 001000 | I |
| 9 | 001001 | J |
| 10 | 001010 | K |
| 11 | 001011 | L |
| 12 | 001100 | M |
| 13 | 001101 | N |
| 14 | 001110 | O |
| 15 | 001111 | P |
| 16 | 010000 | Q |
| 17 | 010001 | R |
| 18 | 010010 | S |
| 19 | 010011 | T |
| 20 | 010100 | U |
| 21 | 010101 | V |
| 22 | 010110 | W |
| 23 | 010111 | X |
| 24 | 011000 | Y |
| 25 | 011001 | Z |
| 26 | 011010 | a |
| 27 | 011011 | b |
| 28 | 011100 | c |
| 29 | 011101 | d |
| 30 | 011110 | e |
| 31 | 011111 | f |
| 32 | 100000 | g |
| 33 | 100001 | h |
| 34 | 100010 | i |
| 35 | 100011 | j |
| 36 | 100100 | k |
| 37 | 100101 | l |
| 38 | 100110 | m |
| 39 | 100111 | n |
| 40 | 101000 | o |
| 41 | 101001 | p |
| 42 | 101010 | q |
| 43 | 101011 | r |
| 44 | 101100 | s |
| 45 | 101101 | t |
| 46 | 101110 | u |
| 47 | 101111 | v |
| 48 | 110000 | w |
| 49 | 110001 | x |
| 50 | 110010 | y |
| 51 | 110011 | z |
| 52 | 110100 | 0 |
| 53 | 110101 | 1 |
| 54 | 110110 | 2 |
| 55 | 110111 | 3 |
| 56 | 111000 | 4 |
| 57 | 111001 | 5 |
| 58 | 111010 | 6 |
| 59 | 111011 | 7 |
| 60 | 111100 | 8 |
| 61 | 111101 | 9 |
| 62 | 111110 | + |
| 63 | 111111 | / |
Padding in the final group
A final group of three bytes takes no padding; two bytes give three characters and one equals sign; one byte gives two characters and two equals signs. RFC 4648 requires the pad characters unless the specification that refers to it says otherwise, so a Base64 string whose length is not a multiple of four is incomplete.
| Bytes in the final group | Characters they fill | Pad |
|---|---|---|
| 3 | 4 | none |
| 2 | 3 | = |
| 1 | 2 | == |
The same value in each encoding
These rows are shared with the other encoders, so a value written here is the value every page in the family writes.
| Value | Written as |
|---|---|
| Hi | text 'Hi' (2 bytes) = base64 'SGk=' |
| Man | text 'Man' (3 bytes) = base64 'TWFu' |
| A | text 'A' (1 byte) = base64 'QQ==' |
| hello | text 'hello' (5 bytes) = base64 'aGVsbG8=' |
Every value below is the one the specification fixes, so a result that disagrees with a row here is a result to check.
| Attribute | Value |
|---|---|
| alphabet | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_ |
| character 62 | - |
| character 63 | _ |
| defining spec | RFC 4648 s5 |
Worked Example: SGk= to Text
One group of bits at a time, with the regrouping drawn out. The same twenty-four cells are cut at eight above and at six below, which is the whole of why 3 bytes become 4 characters.
Result: SGk= = Hi
Start with the first four characters, SGk=. Each one's index in the alphabet is a 6-bit number, and the four numbers written end to end are the bits in the second row. A byte is 8 bits and holds 256 values, 0–255; a nibble is 4 bits. Base64 reads three bytes (24 bits) at a time and writes them as four characters of six bits each, so the output runs four characters for every three bytes - about a third larger than the input.
The second row is the same run of bits cut every 6 instead of every eight. Each six-bit group is a number from 0 to 63, and that number is a row in the table above. Here they are 18, 6, 36, which name S, G, k. Nothing is lost in the regrouping: the same bits are read a different width, which is why the result reverses exactly.
UTF-8 encodes each character in 1 to 4 bytes. Characters 0–127 use one byte identical to ASCII, so ASCII text is valid UTF-8. That is why the byte count, not the character count, decides the length of the result — a character outside US-ASCII takes more than one byte and therefore more than one sixth of a group.
Check it in reverse
The value goes back the way it came. Four characters give their four indices, the indices give 24 bits, and the bits give 3 bytes again. Nothing is carried alongside the result: the alphabet says what each character is worth and the pad says how much of the last group was real. To verify the result by hand, encode the decoded text back to Base64 and compare it with what you started from.
Worked examples: Base64 to text
Each example gives the value first, then the working. Click a value to load it in the converter.
What does SGk= decode to?
SGk= in Base64 is Hi in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does TWFu decode to?
TWFu in Base64 is Man in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does aGVsbG8= decode to?
aGVsbG8= in Base64 is hello in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does QQ== decode to?
QQ== in Base64 is A in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does aGVsbG8gd29ybGQ= decode to?
aGVsbG8gd29ybGQ= in Base64 is hello world in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does Jg== decode to?
Jg== in Base64 is & in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does PA== decode to?
PA== in Base64 is < in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does w6k= decode to?
w6k= in Base64 is é in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does IA== decode to?
IA== in Base64 is (space) in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does cGFzc3dvcmQ= decode to?
cGFzc3dvcmQ= in Base64 is password in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does aVRvb2xIdWI= decode to?
aVRvb2xIdWI= in Base64 is iToolHub in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
What does dXNlcjpwYXNz decode to?
dXNlcjpwYXNz in Base64 is user:pass in text.
Method: read each character's index as 6 bits and regroup the bits into whole bytes.
Every example above reverses exactly - write the same bytes back as six-bit groups.
What Makes a Base64 String Fail to Decode?
Which alphabet is in use?
This page writes the standard alphabet. The URL-safe alphabet of RFC 4648 section 5 keeps the first 62 characters and swaps plus for minus (-) and slash for underscore (_), so the result passes through a URL or a file name unchanged.
Are the line breaks part of the result?
No. RFC 4648 forbids adding line breaks to Base64 output. MIME is the specification that asks for them: RFC 2045 wraps at no more than 76 characters a line and tells decoders to ignore every character that is not in the alphabet.
What can the result be pasted into?
Anything that carries plain US-ASCII text. The alphabet was chosen so that every character in it survives a mail gateway, a JSON string and a form field without being rewritten on the way.
Is this the encoding you need at all?
The three schemes protect different things and are not interchangeable. these encodings are not interchangeable - how these encodings differ sets out which does what.
Base64 to Text in Code
Both standard libraries carry this conversion, so the one-liner below is the whole job. Check the result against the table above before you trust it in a pipeline.
Python: base64.b64decode('SGk=').decode() JavaScript: decodeURIComponent(escape(atob('SGk='))) Common Mistakes
- Treating the result as protection. Base64 exists to carry data through channels that accept only US-ASCII text. It is an encoding, not encryption: any decoder reverses it.
- Losing the pad. RFC 4648 requires the pad characters unless the specification that refers to it says otherwise, so a Base64 string whose length is not a multiple of four is incomplete.
- Pasting a string that carries the other alphabet: a minus sign or an underscore in the middle of a value means it was written for a URL, and this page will name the character it cannot read.
Frequently Asked Questions
How can I tell if something is Base64 encoded?
By its alphabet and its length: 64 characters, at most two equals signs at the end, and a length that is a multiple of four.
Why does a Base64 string fail to decode?
Almost always a character outside the alphabet, or a length that is not a multiple of four. The converter names what it could not read rather than guessing.
Can this decode a URL-safe Base64 value?
It reports the mismatch rather than guessing: a minus sign or an underscore belongs to the other alphabet, and the error line says so.
Does Base64 decoding need the padding?
Yes, by default. RFC 4648 requires the pad characters unless the specification that refers to it says otherwise, so a Base64 string whose length is not a multiple of four is incomplete.
Can you decode Base64 to a PDF or an image?
Yes, when the bytes are a file. A bare Base64 string carries no file type. The media type comes from the data URI prefix, and without one you have to state what the bytes are yourself.
How do you decode Base64 in Python?
base64.b64decode('SGk=').decode() returns Hi. It returns bytes, so the decode() call is what reads them as text.
"Can I go back the other way?" Yes. The same value converts straight back in the text to Base64 direction.
Related Conversions
The rest of the family sits on every encoder and decoder in this family.