A virtual teacher who reveals to you the great secrets of Base64

Base64 Encode Algorithm

The Base64 encode algorithm converts any data into plain text. Technically, it can be said that it converts eight-bit bytes into six-bit bytes. To understand how the encoding algorithm works, check the example below that describes step by step how to manually encode strings to Base64 (if you are looking for an automatic converter, use the Base64 online encoder).

For example, you have the “ABC” string and want to convert it to Base64:

  1. First, you need to split the string letter by letter. Thus, you got 3 groups:
    • A
    • B
    • C

  2. Next you need to convert each group to binary. To do this, for each letter you need to find the corresponding binary value in the ASCII table. Thus, now you have 3 groups of ones and zeros:
    • 01000001
    • 01000010
    • 01000011

  3. Now concatenate all the binary values together (that is, glue all the groups along and make sure you get a total of 24 characters):
    010000010100001001000011

  4. Then, divide the resulting string into groups so that each one has 6 characters (if the last group has less than 6 characters, you need to fill it with zeros until it reaches the desired length). Well and good, now you have 4 groups:
    • 010000
    • 010100
    • 001001
    • 000011

  5. At this step you have to convert six-bit bytes into eight-bit bytes. To do this, prepend the prefix “00” (two zeros) in front of each group:
    • 00010000
    • 00010100
    • 00001001
    • 00000011

  6. There you have to convert each group from binary to decimal by finding its corresponding decimal value in the ASCII table. If you did everything right, each group will be transformed into its integer number as follows:
    • 16
    • 20
    • 9
    • 3

  7. Integer numbers obtained in the previous step are called “Base64 indices”. They are easy to remember, because it is a zero-based numbering, where each index corresponds to a Latin letter. It starts with the letter “A” in alphabetical order (i.e., A=0, B=1, C=2, D=3, and so on). For complete list, see Base64 Characters Table. So, matching indexes, convert them to corresponding letters:
    • Q
    • U
    • J
    • D

  8. The final chord, concatenate all letters to get the Base64 string:
    QUJD

To summarize, you learned that encoding “ABC” to Base64 yields the result “QUJD”. As you can see, this is a very simple process and you can encode text to Base64 even by hand. I hope that you managed to get the right encoding result. Otherwise, let me know and I will try to help you.

If you need more step by step examples, use the form below to get encoding instructions for custom strings (once you submit the form, the article above will be updated accordingly in real time):

Comments (35)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Ian,
    Hi there,

    I am stuck in encoding from hex to base64 in Android. Would you please show me how? I tried

    Base64.encode(hexString.getBytes(), Base64.DEFAULT); and Base64.encodeToString but am not successful yet.

    Best regards,

    Ian
    • Abel_Matos,
      Here many responses:
      https://stackoverflow.com/questions/7372268/how-to-convert-hex-to-base64
  • ahdilaw,
    how to encode a file manually into base64?
    • Administrator,
      The algorithm is the same, except you should open your file in a hex/binary viewer in order to get binary values for each character.
  • Dera,
    Bonjour! Merci pour votre aide
  • Rowan,
    Amazing! beautiful! I love this oh my, thank you for this explanation! I can't express my gratitude enough!
    • Administrator,
      Hey Rowan! I am glad that you found it useful, and I really appreciate your kind words.
  • b_k,
    Awesome and helpful. Mr base64 guru. And i think u r really base64 guru.
  • b_k,
    sir, why used padding like '==' or '=' in base64 function. Can i replace this '=' padding by 'tilde' with php function 'strtr' as '+/=' to '-_~', in your base64url function; though it is not mandatory acording to your explantion on this topic. Just want to know for my curiosity.
    • Administrator,
      Hello. The padding character is optional, so you can remove it (for example, using the rtrim() function). If you plan to replace it (=) with another character after encoding, you must reverse the process before decoding.
  • Abhinav,
    What if I simply want to convert 'A'. How will algorith deal with a single character and not a string. I saw decoders converting it to 'QQ==' but if I do it manually I can't approach 'Q' even if I follow the algo. Am I doing something wrong ?
    • Administrator,
      I think that you are doing something wrong at the step when you have to fill the last group with zeros. For detailed instructions, I recommend you to take a look at the end of the article ;)
  • lowy,
    Step2:"Next you need to convert each group to binary. To do this, for each character you need to find the corresponding binary value in the ASCII table."
    Convert stuff lower or equals to 127 is fine, but how about higher than that? lets say the character '¤', according to ASCII Table the corresponding binary value is `10100100` (which is 145 in decimal) but in here I got `1100001010100100`, what's happening here?
    how do you convert any character that has a decimal value higher than 127?
    • Administrator,
      Hello lowy!
      Thank you for your message and I'm sorry for confusing you.

      The problem is that the extended ASCII table (decimal > 127) and the encoder provided on this page use different character encodings. Instead of the extended ASCII table, please use the converter provided on the ASCII Table page.

      Meanwhile I'll try to figure out how to make it clear for everyone (perhaps the best option is to remove the extended ASCII table, because it can also be confusing at decoding).
      • lowy,
        do you mind telling me how do you encode the extended ASCII Table? like is it according to Unicode or something? or did you create your own encoder? I don't mind seeing code (I know a little bit about JavaScript and in fact I am trying to create a function that encodes Strings to Base64)
        • Administrator,
          The extended ASCII Table on that page uses the Windows-1252 character encoding, while the convertor uses the UTF-8.

          By the way, it doesn’t matter the character encoding as long as you use the same character set for encoding and decoding. For example, if you use Windows-1252 table to encode A¤B to Base64 you’ll get the QaRC result. And you can decode it back to the original string if you still use Windows-1252. But if you try to decode QaRC using the CP850 character set, it will be decoded as AñB. For more differences, try to decode QaRC using the Character Encoding Detection tool.
  • Thomas,
    Hello,
    So I've been really interested in the Advanced Encryption Standard, but it's kind of hard because I'm not the best coder ever. When I've discovered Base64, I've been really interested in it. But is there a way to encode with a password kind of like AES (Advanced Encryption Standard)? I read that Base64 isn't the best encoding algorithm as people can easily decode it.
    Thanks
  • Luke,
    Could you explain how this works with images? It seems like images should contain some kind of information explaining what they are and maybe their dimensions and then how the pixel colors are recorded
  • Cavinato,
    quando faço o encode base 64 de uma imagem (ex. png). o inicio " P1BORw" que deve ser isso ?PNG como posso tirar isso ?
    que não tem quando vejo na sua base a mesma imagem


    Function Base64Encode(sText)
        Dim oXML, oNode
    Set oXML             = Server.CreateObject("MSXML2.DomDocument")
        Set oNode            = oXML.CreateElement("b64")
        oNode.DataType       = "bin.base64"
        oNode.Text           = Mid(base64String, InStr(base64String, ",") + 1)
    oNode.nodeTypedValue = Stream_StringToBinary(sText)  
        Base64Encode         = oNode.text
    Set oNode            = nothing
        Set oXML             = nothing
    End Function 

    Private Function Stream_StringToBinary(Text)
      Const adTypeText =  2
      Const adTypeBinary =  1
      Dim BinaryStream ' como novo fluxo  
      Set BinaryStream = CreateObject("ADODB.Stream")

      'Specify stream type - we want To save text/string data.
      BinaryStream.Type = adTypeText

      BinaryStream.CharSet  =  "us-ascii"

      'Open the stream And write text/string data To the object
      BinaryStream.Open
      BinaryStream.WriteText = Text

      'Change stream type To binary
      BinaryStream.Position = 0
      BinaryStream.Type     = adTypeBinary

      'Ignore first two bytes - sign of
      BinaryStream.Position =  0

      'Open the stream And get binary data from the object
      Stream_StringToBinary = BinaryStream.Read
      Set BinaryStream =  nothing
    End Function

  • Saleem,
    how to do last = or == , i am conveting hex decimal to base64 all is ok but last character some time = or == compary with your convertion
  • Mr_Meeseeks,
    Superb site... Thank you for sharing your knowledge
  • nqhXncMU,
    7QZRbM1a') OR 506=(SELECT 506 FROM PG_SLEEP(15))--
  • nqhXncMU,
    kwQW4l1g')) OR 798=(SELECT 798 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    -1 OR 2+239-239-1=0+0+0+1 --
  • gBqsPxAZ,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • gBqsPxAZ,
    -5 OR 799=(SELECT 799 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • gBqsPxAZ,
    -1); waitfor delay '0:0:15' --
  • nqhXncMU,
    h7K8M4Gc') OR 478=(SELECT 478 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1' OR 2+564-564-1=0+0+0+1 --
  • nqhXncMU,
    kRKIpUAM')) OR 144=(SELECT 144 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -5) OR 325=(SELECT 325 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    -1" OR 3+854-854-1=0+0+0+1 --
  • ncMUFCMU,
    jWBtnjUl')) OR 913=(SELECT 913 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.