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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With