Base64 Decode: Base64 to Text

SGk=
18537
48 69

text

Swap to text to Base64

Encode text as Base64

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.

In short

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.

  1. Strip any line breaks. A decoder ignores every character that is not in the alphabet.
  2. Take the characters 4 at a time and look each one up: its index is a 6-bit number.
  3. Join the 4 numbers into 24 bits and read them as 3 bytes.
  4. Drop the bits a = stands for; they carry nothing.
  5. 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 _.

Base64 to Text Conversion Table
IndexSix bitsCharacter
0000000A
1000001B
2000010C
3000011D
4000100E
5000101F
6000110G
7000111H
8001000I
9001001J
10001010K
11001011L
12001100M
13001101N
14001110O
15001111P
16010000Q
17010001R
18010010S
19010011T
20010100U
21010101V
22010110W
23010111X
24011000Y
25011001Z
26011010a
27011011b
28011100c
29011101d
30011110e
31011111f
32100000g
33100001h
34100010i
35100011j
36100100k
37100101l
38100110m
39100111n
40101000o
41101001p
42101010q
43101011r
44101100s
45101101t
46101110u
47101111v
48110000w
49110001x
50110010y
51110011z
521101000
531101011
541101102
551101113
561110004
571110015
581110106
591110117
601111008
611111019
62111110+
63111111/

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.

Padding in the final group
Bytes in the final groupCharacters they fillPad
34none
23=
12==

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.

The same value in each encoding
ValueWritten as
Hitext 'Hi' (2 bytes) = base64 'SGk='
Mantext 'Man' (3 bytes) = base64 'TWFu'
Atext 'A' (1 byte) = base64 'QQ=='
hellotext '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.

Base64 at a glance
AttributeValue
alphabetABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
character 62-
character 63_
defining specRFC 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.

bytes
0100100001101001
sextets
010010000110100100
value
18636
Base64
SGk =

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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.

Load this value in the converter

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

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.

The rest of the family sits on every encoder and decoder in this family.