📜  C#| Convert.ToBase64String()方法|套装1

📅  最后修改于: 2021-05-29 19:36:11             🧑  作者: Mango

Convert.ToBase64String()方法用于将8位无符号整数数组的值转换为其等效的字符串表示形式,该字符串表示形式以base-64数字编码。此方法的重载中有4种方法,如下所示:

  • ToBase64String(Byte [],Int32,Int32)方法
  • ToBase64String(Byte [],Int32,Int32,Base64FormattingOptions)方法
  • ToBase64String(Byte [],Base64FormattingOptions)方法
  • ToBase64String(Byte [])方法

在这里,我们将只讨论一种方法。

ToBase64String(Byte [],Int32,Int32)方法用于将8位无符号整数数组的子集转换为等效的字符串表示形式,该字符串表示形式以base-64数字编码。参数将子集指定为输入数组中的偏移量,并指定要转换的数组中元素的数量。

句法:

public static string ToBase64String (byte[] inArray, int offset, int length);

参数:

  • inArray :这是一个8位无符号整数的数组。
  • offset :它是inArray中的偏移量。
  • length :它是要转换的inArray的元素数。

返回值:该方法以inArray的in元素的长度为64的字符串表示形式返回,从位置偏移量开始。

例外情况:

  • ArgumentNullException:如果inArray为null。
  • ArgumentOutOfRangeException:如果offsetlength为负或OR offset plus length大于inArray的长度。

下面的程序说明了Convert.ToBase64String(Byte [],Int32,Int32)方法的使用:

范例1:

// C# program to demonstrate the
// Convert.ToBase64String() Method
using System;
  
class GFG {
  
// Main Method
public static void Main()
{
    try {
  
        // defining and initializing
        // byte1 and byte2
        byte[] byte1 = {2, 4, 6, 8, 10,
                   12, 14, 16, 18, 20};
  
        byte[] byte2 = {10, 20, 30, 40, 50};
  
        // calling get() Method
        get(byte1, "byte1");
  
        Console.WriteLine("");
        get(byte2, "byte2");
    }
  
    catch (ArgumentNullException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
  
    catch (ArgumentOutOfRangeException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(),
                    e.Message);
    }
}
  
// Defining get() method
public static void get(byte[] bytes, string str)
{
    Console.WriteLine("For {0}", str);
  
    // converting byte to base 64 string
    string val = Convert.ToBase64String(bytes,
                             0, bytes.Length);
  
    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
输出:
For byte1
converted string: AgQGCAoMDhASFA==

For byte2
converted string: ChQeKDI=

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Convert.ToBase64String() Method
using System;
  
class GFG {
  
// Main Method
public static void Main()
{
    try {
  
        // defining and initializing
        // byte1
        byte[] byte1 = {2, 4, 6, 8, 10,
                   12, 14, 16, 18, 20};
  
        // calling get() Method
        get(byte1, "byte1");
  
        Console.WriteLine("");
  
        // converting base 64 string 
        // to byte array
        Console.WriteLine("bye array is null");
  
        string val = Convert.ToBase64String(null, 0, 10);
        Console.WriteLine("Converted byte value: {0}", val);
    }
  
    catch (ArgumentNullException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
  
    catch (ArgumentOutOfRangeException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(),
                    e.Message);
    }
}
  
// Defining get() method
public static void get(byte[] bytes, string str)
{
    Console.WriteLine("For {0}", str);
  
    // converting byte to base 64 string
    string val = Convert.ToBase64String(bytes, 
                             0, bytes.Length);
  
    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
输出:
For byte1
converted string: AgQGCAoMDhASFA==

bye array is null
Exception Thrown: System.ArgumentNullException

示例3:对于ArgumentOutOfRangeException

// C# program to demonstrate the
// Convert.ToBase64String() Method
using System;
  
class GFG {
  
// Main Method
public static void Main()
{
    try {
  
        // defining and initializing
        // byte1
        byte[] byte1 = {2, 4, 6, 8, 10,
                   12, 14, 16, 18, 20};
  
        // calling get() Method
        get(byte1, "byte1");
  
        Console.WriteLine("");
  
        // converting base 64 string to byte array
        Console.WriteLine("Length is negative");
  
        string val = Convert.ToBase64String(byte1, 0, -10);
          
        Console.WriteLine("Converted byte value: {0}", val);
    }
  
    catch (ArgumentNullException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
  
    catch (ArgumentOutOfRangeException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(),
                    e.Message);
    }
}
  
// Defining get() method
public static void get(byte[] bytes, string str)
{
    Console.WriteLine("For {0}", str);
  
    // converting byte to base 64 string
    string val = Convert.ToBase64String(bytes,
                             0, bytes.Length);
  
    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
输出:
For byte1
converted string: AgQGCAoMDhASFA==

Length is negative
Exception Thrown: System.ArgumentOutOfRangeException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.convert.tobase64string?view=netframework-4.7.2#System_Convert_ToBase64String_System_Byte___System_Int32_System_Int32_