public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
char[] outArray, int offsetOut)
{
if (inArray == null)
throw new ArgumentNullException ("inArray");
if (outArray == null)
throw new ArgumentNullException ("outArray");
if (offsetIn < 0 || length < 0 || offsetOut < 0)
throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
// avoid integer overflow
if (offsetIn > inArray.Length - length)
throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
// note: normally ToBase64Transform doesn't support multiple block processing
byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
// avoid integer overflow
if (offsetOut > outArray.Length - cOutArr.Length)
throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
return cOutArr.Length;
}