vanpersil
vanpersil

Reputation: 814

How to combine two base64 strings into one

I get files from an external system represented in base64 strings. The files are of various formats: docx, pdfs, jpg etc. Most of the time, I get them as one base64 string. In such cases, I can convert them back to binary files. Sometimes, due to some limitations of the aforementioned system, I get the files as two or more base64 strings. How, then, should I join them to one array of bytes[]

public static byte[] Join64Strings(string[] base64Strings)
{
    var bytes = new List<byte>();
    foreach (var base64String in base64Strings)
    {
        bytes.AddRange(Convert.FromBase64String(base64String));
    }
    return bytes.ToArray();
}

If I have one element in base64Strings array, it works. The problem is with more than one element. Then the file is written with

File.WriteAllBytes

is corrupted. I think the problem is with terminating = (equal signs) which appears at the end of all the strings in the array. Maybe they need to be handled in some special way.

Upvotes: 2

Views: 3180

Answers (1)

K J
K J

Reputation: 11830

should I join them to one array of bytes" ? NO

The chances of direct merge working is variable (majority not)

It would not make sense unless each was a partial file (see below), the = (most but not all) end of file padding should be one of several indications that each is a discreet file object (ideally each should have a name to convert to otherwise you don't know mime file type (the reply should indicate if you need to save as multiple jpeg docx or pdf)

Thus it is not generally possible to mash jpeg to pdf to docx that should set off antiviral behaviour detected klaxon.

Base64 is based on converting blocks of 3 bytes into 4 so if the combined files were correctly split on boundaries of 3bytes they could be simply concatenated as one.

And for that you would think ok I divide base64 by 4 to work out blocks of 3 however if source blocks were say 1024, (or other common binary division) that would not be encoded as 3's since there would be a remainder which needs some padding of last fragment (often the = character.)

So the answer to adding base64 chunks is 1st reverse to separate bytestreams then concatenate the byte blocks to make a whole bytestream.

Upvotes: 1

Related Questions