Calculate actual data size from Base64 encoded string length

15,263

The exact length cannot be calculated unless you look at the padding. Without looking for padding, the best you can do is calculate an upper bound for the length by multiplying the encoded-string length with 3/4 (the encoded length is guaranteed to be exactly divisible by 4).

The upper bound calculated thus will be either N, N+1 or N+2, where N is the length of the raw data.

Share:
15,263

Related videos on Youtube

b_erb
Author by

b_erb

UUlm

Updated on June 01, 2022

Comments

  • b_erb
    b_erb almost 2 years

    Given data encoded as a Base64-encoded string, can I somehow calculate the actual length of the raw data that has been encoded only by looking at the length of the Base64-encoded string?

    I don't want to traverse the string if not necessary (this also includes string operations on the trailling characters of the encoded string to check for padding).