Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a byte[] array ever not convert to a Base64 string?

Is there any combination of a byte[] that will cause this to fail, other than byteArray being null?

var myString = Convert.ToBase64String(byteArray);

I'm thinking something like... starting or ending with byte zero (null?). An Empty byte[]? A very large byte[]? Some sequence of bytes that wouldn't make sense in Base64? My issue is the unknown unknowns.

like image 961
Suamere Avatar asked Dec 21 '25 04:12

Suamere


2 Answers

Is there any combination of a byte[] that will cause this to fail

byte[] byteArray = null;
var myString = Convert.ToBase64String(byteArray);
// NullArgumentException.

other than byteArray being null?

Nope.

starting or ending with byte zero (null?)

Ending in 0:

var byteArray = new byte[] {255, 255, 0};
var myString = Convert.ToBase64String(byteArray);
Console.WriteLine(myString); // +voA

Leading 0:

var byteArray = new byte[] {0, 250, 250};
var myString = Convert.ToBase64String(byteArray);
Console.WriteLine(myString); // APr6

By the way...

var byteArray = new byte[] {250, 250};
var myString = Convert.ToBase64String(byteArray);
Console.WriteLine(myString); // +vo=

No, it is not just ignoring the 0.

An Empty byte[]?

var byteArray = new byte[] {};
var myString = Convert.ToBase64String(byteArray);
Console.WriteLine(myString.Length); // 0

Empty string.

A very large byte[]?

Ern...

var byteArray = new byte[int.MaxValue]; // OutOfMemoryException

Hmm...

var byteArray = new byte[int.MaxValue / 8];
var myString = Convert.ToBase64String(byteArray);
Console.WriteLine(myString.Length); // 357913940

It survived.

Some sequence of bytes that wouldn't make sense in Base64?

No such thing exists. You take the the input as binary (base 2), do base conversion to base 64, and follow the standard for representation.


My issue is the unknown unknowns.

In general, stick to the documentation and do no try to do something that is not documented.

I suppose there is a chance that there could be a bug in the platform. There are some bugs, none in ToBase64String that I know of. However - in general - it is not your responsability to fix them or work around them (sometimes you will have to). If you happen to stumble upon one, report it.

The thing with bugs is that they are unknown unknowns until discovered. You can try to have a look here and here. Beyond that, how will I be able to point you to them if I do not know them?

Oh, you want to find bugs? Alright, test (as I did for this answer), and review the source (reference source, .NET Core). Hey, they are using ReadOnlySpan, I guess that is why it did not blow up with the large array.

like image 51
Theraot Avatar answered Dec 23 '25 17:12

Theraot


Is there any combination of a byte[] that will cause this to fail, other than byteArray being null?

As stated on the docs: no. Exception-wise the Convert.ToBase64String overload you are using only throws ArgumentNullException (when its parameter inArray is null).

like image 24
andresantacruz Avatar answered Dec 23 '25 19:12

andresantacruz