Calculate the size to a Base 64 decoded message

I have a BASE64 encode string:

static const unsigned char base64_test_enc[] =
    "VGVzdCBzdHJpbmcgZm9yIGEgc3RhY2tvdmVyZmxvdy5jb20gcXVlc3Rpb24=";

It does not have CRLF-per-72 characters.

How to calculate a decoded message length?

Jon Skeet
people
quotationmark

Well, base64 represents 3 bytes in 4 characters... so to start with you just need to divide by 4 and multiply by 3.

You then need to account for padding:

  • If the text ends with "==" you need to subtract 2 bytes (as the last group of 4 characters only represents 1 byte)
  • If the text ends with just "=" you need to subtract 1 byte (as the last group of 4 characters represents 2 bytes)
  • If the text doesn't end with padding at all, you don't need to subtract anything (as the last group of 4 characters represents 3 bytes as normal)

people

See more on this question at Stackoverflow