11

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).

1

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.