Smallwikipedialogo.png Wikipedia has an article related to:

Base64 is a method used to convert binary data into a string. It is so called because it uses a set of 64 characters to encode each sequence of six bits, similar to a mathematical base 64.


In Base64, 0 to 25 correspond to A to Z, 26 to 51 correspond to (lowercase) a to z, 52 to 61 correspond to digits 0 to 9, 62 corresponds to the plus sign "+", and 63 corresponds to the slash sign "/". The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence.

Each group of six bits is encoded using the above conversion. There are also three cases to consider depending on the size of the input: when the size in bytes is divisible by 3 (bits divisible by 24), with an extra byte (8 bits extra, or remainder of 1 byte when divided by three) and with two extra bytes

  • The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.
  • The size has an extra byte (remainder of 8 bits when divided by 24): Bits that can be encoded in the beginning are encoded normally. This leaves us with one byte (8 bits); we pad the remaining byte with two bytes with value zero and encode the last sequence. Two equal signs ("==") are added to the encoded string.
  • The size has extra two bytes (remainder of 16 bits when divided by 24): Same as above, but we pad just one byte. One equal sign ("=") is added to the encoded string.

Depending on the format (for instance, this is required in MIME), a newline is inserted every 76 characters in the encoded string.


This example encodes the string "Hello" in ASCII and then in Base64.

Original H e l l o (padding)
ASCII (hex) 4 8 6 5 6 C 6 C 6 F
Byte sequence 010010000110010101101100 0110110001101111 00000000
Base64 S G V s b G 8

The final Base64 string is "SGVsbG8=".

Try "Hello!":

Original l o  !
ASCII (hex) 6 C 6 F 2 1
Byte sequence 0110110001101111 00100001
Base64 b G 8 h

The final Base64 string is "SGVsbG8h".