System.Convert.ToBase64CharArray 方法 (Byte[], Int32, Int32, Char[], Int32)

方法描述

将 8 位无符号整数数组的子集转换为用 Base64 数字编码的 Unicode 字符数组的等价子集。 参数将子集指定为输入和输出数组中的偏移量和输入数组中要转换的元素数。

语法定义(C# System.Convert.ToBase64CharArray 方法 (Byte[], Int32, Int32, Char[], Int32) 的用法)

public static int ToBase64CharArray(
	byte[] inArray,
	int offsetIn,
	int length,
	char[] outArray,
	int offsetOut
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
inArray System-Byte[] 8 位无符号整数的输入数组。
offsetIn System-Int32 inArray 内的一个位置。
length System-Int32 要转换的 inArray 的元素数。
outArray System-Char[] Unicode 字符的输出数组。
offsetOut System-Int32 outArray 内的一个位置。
返回值 System.Int32 包含 outArray 中的字节数的 32 位带符号整数。

提示和注释

inArray 中从位置 offsetIn 开始的 length 元素的子集作为一个数字值,并且转换为 outArray 中从位置 offsetOut 开始的元素的子集。 返回值指示 outArray 中被转换的元素的数目。 outArray 的子集由 Base 64 数字组成。

从零开始升序排列的 Base 64 数字分别为大写字符“A”到“Z”、小写字符“a”到“z”、数字“0”到“9”以及符号“+”和“/”。 无值字符“=”用于尾部的空白。

offset 和 length 参数是 32 位有符号数字。 offsetIn 和 offsetOut 参数为从零开始的数组位置。

重要事项

该 ToBase64CharArray 方法旨在处理包含要编码的所有数据的单字节数组。 若要从字节流创建基于 64 字符的数组,可使用 System.Security.Cryptography.ToBase64Transform类。

System.Convert.ToBase64CharArray 方法 (Byte[], Int32, Int32, Char[], Int32)例子

下面的示例演示如何使用 ToBase64CharArray 方法对二进制流进行 UUencode (base 64) 编码,然后将编码保存到文件中。

public void EncodeWithCharArray() {
   System.IO.FileStream inFile;    
   byte[]             binaryData;

   try {
      inFile = new System.IO.FileStream(inputFileName,
                                 System.IO.FileMode.Open,
                                System.IO.FileAccess.Read);
      binaryData = new Byte[inFile.Length];
      long bytesRead = inFile.Read(binaryData, 0,
                           (int) inFile.Length);
      inFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or reading from it.
      System.Console.WriteLine("{0}", exp.Message);
      return;
   }

   // Convert the binary input into Base64 UUEncoded output.
   // Each 3 byte sequence in the source data becomes a 4 byte
   // sequence in the character array. 
   long arrayLength = (long) ((4.0d/3.0d) * binaryData.Length);

   // If array length is not divisible by 4, go up to the next
   // multiple of 4.
   if (arrayLength % 4 != 0) {
      arrayLength += 4 - arrayLength % 4;
   }

   char[] base64CharArray = new char[arrayLength];
   try {
      System.Convert.ToBase64CharArray(binaryData, 
                               0,
                               binaryData.Length,
                               base64CharArray,
                               0);
   }
   catch (System.ArgumentNullException) {
      System.Console.WriteLine("Binary data array is null.");
      return;
   }
   catch (System.ArgumentOutOfRangeException) {
      System.Console.WriteLine("Char Array is not large enough.");
      return;
   }

   // Write the UUEncoded version to the output file.
   System.IO.StreamWriter outFile; 
   try {
      outFile = new System.IO.StreamWriter(outputFileName,
                              false,
                              System.Text.Encoding.ASCII);          
      outFile.Write(base64CharArray);
      outFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or writing to it.
      System.Console.WriteLine("{0}", exp.Message);
   }
}

异常

异常 异常描述
ArgumentNullException inArray 或 outArray 为 null。
ArgumentOutOfRangeException
  • offsetIn、offsetOut 或 length 为负。
  • offsetIn 加上 length 大于 inArray 的长度。
  • offsetOut 加上要返回的元素数大于 outArray 的长度。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。