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

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

此方法用于将8位无符号整数数组的子集转换为以64位为基数编码的Unicode字符数组的等效子集。参数将子集指定为输入和输出数组中的偏移量,以及输入数组中要转换的元素数。

句法:

参数:

  • inArray :一个8位无符号整数的输入数组。
  • offsetIninArray中的位置。
  • length :要转换的inArray的元素数。
  • outArray :Unicode字符的输出数组。
  • offsetOutoutArray中的位置。

返回值:该方法返回一个32位带符号整数,其中包含outArray中的字节

例外情况:

  • ArgumentNullException :如果inArrayoutArray为null。
  • ArgumentOutOfRangeException :如果offsetInoffsetOutlength为负数,或者offsetIn加上length大于inArray的长度,或者offsetOut加上要返回的元素数大于outArray的长度。

下面的程序说明了Convert.ToBase64CharArray()方法的用法:

范例1:

// C# program to demonstrate the
// Convert.FromBase64String(String) 
// 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);
  
    long arrayLength = (long)((4.0d / 3.0d) *
                               bytes.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;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("");
}
}
输出:
For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

For byte2
Total no of bytes: 8
base64CharArray: ChQeKDI=

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Convert.FromBase64String(String)
// 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");
  
        // converting base 64 
        // string to byte array
        Console.WriteLine("inArray and outArray are null.");
  
        int val = Convert.ToBase64CharArray(null, 0,
                                       10, null, 0);
                                         
        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);
  
    long arrayLength = (long)((4.0d / 3.0d) * 
                               bytes.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;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("\n");
}
}
输出:
For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

inArray and outArray are null.
Exception Thrown: System.ArgumentNullException

示例3:对于ArgumentOutOfRangeException

// C# program to demonstrate the
// Convert.FromBase64String(String)
// 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");
  
        // converting base 64 string
        // to byte array
        Console.WriteLine("length of inArray is negative");
  
        char[] base64CharArray = new char[10];
        int val = Convert.ToBase64CharArray(byte2, 0,
                            -10, base64CharArray, 0);
                              
        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);
  
    long arrayLength = (long)((4.0d / 3.0d) * bytes.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;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("\n");
}
}
输出:
For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

length of inArray is negative
Exception Thrown: System.ArgumentOutOfRangeException

参考:

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