System.Convert.ToBase64String 方法 (Byte[])

方法描述

将 8 位无符号整数的数组转换为其用 Base64 数字编码的等效字符串表示形式。

语法定义(C# System.Convert.ToBase64String 方法 (Byte[]) 的用法)

public static string ToBase64String(
	byte[] inArray
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
inArray System-Byte[] 一个 8 位无符号整数数组。
返回值 System.String inArray 的内容的字符串表示形式,以 Base64 表示。

提示和注释

inArray 的元素用作数字值,并且转换为使用 base 64 数字编码的字符串表示形式。

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

重要事项

该 ToBase64String 方法旨在处理包含要编码的所有数据的单字节数组。 要从流对数据进行编码,请使用 System.Security.Cryptography.ToBase64Transform 类。

System.Convert.ToBase64String 方法 (Byte[])例子

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

public void EncodeWithString() {
   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.
   string base64String;
   try {
       base64String = 
         System.Convert.ToBase64String(binaryData, 
                                0,
                                binaryData.Length);
   }
   catch (System.ArgumentNullException) {
      System.Console.WriteLine("Binary data array is null.");
      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(base64String);
      outFile.Close();
   }
   catch (System.Exception exp) {
      // Error creating stream or writing to it.
      System.Console.WriteLine("{0}", exp.Message);
   }
}

异常

异常 异常描述
ArgumentNullException inArray 为 null。

命名空间

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 系统要求。