閱讀英文

共用方式為


Convert.ToBase64CharArray 方法

定義

將 8 位元不帶正負號的整數陣列的子集,轉換為相等的 Base-64 位數編碼的 Unicode 字元陣列子集。

多載

ToBase64CharArray(Byte[], Int32, Int32, Char[], Int32, Base64FormattingOptions)

將 8 位元不帶正負號的整數陣列的子集,轉換為相等的 Base-64 位數編碼的 Unicode 字元陣列子集。 參數會指定可在輸入和輸出陣列中當成位移 (Offset) 的子集、輸入陣列中要轉換的項目個數,以及是否要在輸出陣列中插入分行符號。

ToBase64CharArray(Byte[], Int32, Int32, Char[], Int32)

將 8 位元不帶正負號的整數陣列的子集,轉換為相等的 Base-64 位數編碼的 Unicode 字元陣列子集。 參數會將子集指定為輸入和輸出陣列中的位移,以及轉換輸入陣列中的項目數目。

ToBase64CharArray(Byte[], Int32, Int32, Char[], Int32, Base64FormattingOptions)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

將 8 位元不帶正負號的整數陣列的子集,轉換為相等的 Base-64 位數編碼的 Unicode 字元陣列子集。 參數會指定可在輸入和輸出陣列中當成位移 (Offset) 的子集、輸入陣列中要轉換的項目個數,以及是否要在輸出陣列中插入分行符號。

C#
public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options);

參數

inArray
Byte[]

8 位元不帶正負號的整數的輸入陣列。

offsetIn
Int32

inArray 中的位置。

length
Int32

要轉換的 inArray 項目數目。

outArray
Char[]

Unicode 字元的輸出陣列。

offsetOut
Int32

outArray 中的位置。

options
Base64FormattingOptions

InsertLineBreaks 會在每 76 個字元之後插入分行符號;None 則不會插入分行符號。

傳回

32 位元帶正負號的整數,包含 outArray 中的位元數。

屬性

例外狀況

inArrayoutArraynull

offsetInoffsetOutlength 為負。

-或-

offsetIn 加上 length 大於 inArray 的長度。

-或-

offsetOut 加上要傳回的項目數目大於 outArray的長度。

options 不是有效的 Base64FormattingOptions 值。

範例

下列範例示範 ToBase64CharArray 方法。 輸入會分成三個字節的群組, (24 位) 每個位元組。 因此,每個群組都包含四個 6 位數位,其中每個數位的範圍從十進位 0 到 63。 在此範例中,有85個3位元組的群組剩餘一個字節。 第一個群組包含十六進位值 00、01 和 02,這會產生四個 6 位值等於十進位 0、0、4 和 2。 這四個值會對應至輸出開頭的基底 64 位數 “A”、“A”、“E” 和 “C”。

如果 3 位元組群組的整數數目不存在,剩餘的位元組會有效地填補零,以形成完整的群組。 在此範例中,最後一個字節的值是十六進位 FF。 前 6 個位等於 decimal 63,其對應於輸出結尾的 base-64 digit “/”,而接下來 2 個位會填補零,以產生十進位 48,其對應於 base-64 digit “w”。 最後兩個 6 位值會填補,並對應至無值填補字元 “=”。

C#
// This example demonstrates the Convert.ToBase64CharArray() and
//                               Convert.FromBase64CharArray methods

using System;

class Sample
{
    public static void Main()
    {
    byte[] byteArray1 = new byte[256];
    byte[] byteArray2 = new byte[256];
    char[] charArray  = new char[352];
    int charArrayLength;
    string nl = Environment.NewLine;

    string ruler1a = "         1         2         3         4";
    string ruler2a = "1234567890123456789012345678901234567890";
    string ruler3a = "----+----+----+----+----+----+----+----+";
    string ruler1b = "         5         6         7      ";
    string ruler2b = "123456789012345678901234567890123456";
    string ruler3b = "----+----+----+----+----+----+----+-";
    string ruler   = String.Concat(ruler1a, ruler1b, nl,
                                   ruler2a, ruler2b, nl,
                                   ruler3a, ruler3b);

// 1) Initialize and display a Byte array of arbitrary data.
    Console.WriteLine("1) Input: A Byte array of arbitrary data.{0}", nl);
    for (int x = 0; x < byteArray1.Length; x++)
    {
    byteArray1[x] = (byte)x;
    Console.Write("{0:X2} ", byteArray1[x]);
    if (((x+1)%20) == 0) Console.WriteLine();
    }
    Console.Write("{0}{0}", nl);

// 2) Convert the input Byte array to a Char array, with newlines inserted.
    charArrayLength =
        Convert.ToBase64CharArray(byteArray1, 0, byteArray1.Length,
                                   charArray, 0, Base64FormattingOptions.InsertLineBreaks);
    Console.WriteLine("2) Convert the input Byte array to a Char array with newlines.");
    Console.Write("   Output: A Char array (length = {0}). ", charArrayLength);
    Console.WriteLine("The elements of the array are:{0}", nl);
    Console.WriteLine(ruler);
    Console.WriteLine(new String(charArray));
    Console.WriteLine();

// 3) Convert the Char array back to a Byte array.
    Console.WriteLine("3) Convert the Char array to an output Byte array.");
    byteArray2 = Convert.FromBase64CharArray(charArray, 0, charArrayLength);

// 4) Are the input and output Byte arrays equivalent?
    Console.WriteLine("4) The output Byte array is equal to the input Byte array: {0}",
                      ArraysAreEqual(byteArray1, byteArray2));
    }

    public static bool ArraysAreEqual(byte[] a1, byte[] a2)
    {
    if (a1.Length != a2.Length) return false;
    for (int i = 0; i < a1.Length; i++)
        if (a1[i] != a2[i]) return false;
    return true;
    }
}
/*
This example produces the following results:

1) Input: A Byte array of arbitrary data.

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13
14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27
28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B
3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77
78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B
8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3
B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7
C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB
DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

2) Convert the input Byte array to a Char array with newlines.
   Output: A Char array (length = 352). The elements of the array are:

         1         2         3         4         5         6         7
1234567890123456789012345678901234567890123456789012345678901234567890123456
----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4
OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx
cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq
q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj
5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==

3) Convert the Char array to an output Byte array.
4) The output Byte array is equal to the input Byte array: True

*/

備註

從位置 offsetIn開始的參數元素lengthinArray子集會當做數值,並轉換成參數中outArray從位置 offsetOut開始的元素子集。 傳回值表示 中的 outArray已轉換項目數目。 的 outArray 子集包含base-64位數。

以零為基底 64 位數的遞增順序是大寫字元 “A” 到 “Z”、小寫字元 “a” 到 “z”、數位 “0” 到 “9”,以及符號 “+” 和 “/”。 無值字元 「=」 用於尾端填補。

offsetlength 參數是32位帶正負號的數位。 offsetInoffsetOut 參數是以零起始的陣列位置。

重要

方法 ToBase64CharArray 的設計目的是要處理包含要編碼之所有數據的單一位元組陣列。 若要從位元組數據流建立base-64字元陣列,請使用 System.Security.Cryptography.ToBase64Transform 類別。

options如果 參數設定為 InsertLineBreaks ,且轉換的輸出長度超過 76 個字元,則會每隔 76 個字元插入換行符。 換行符定義為歸位字元, (U+000D) 後面接著換行字元, (U+000A) 。 如需詳細資訊,請參閱 RFC 2045,網址為 https://www.rfc-editor.org/「多用途因特網郵件延伸模組」。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToBase64CharArray(Byte[], Int32, Int32, Char[], Int32)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

將 8 位元不帶正負號的整數陣列的子集,轉換為相等的 Base-64 位數編碼的 Unicode 字元陣列子集。 參數會將子集指定為輸入和輸出陣列中的位移,以及轉換輸入陣列中的項目數目。

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

參數

inArray
Byte[]

8 位元不帶正負號的整數的輸入陣列。

offsetIn
Int32

inArray 中的位置。

length
Int32

要轉換的 inArray 項目數目。

outArray
Char[]

Unicode 字元的輸出陣列。

offsetOut
Int32

outArray 中的位置。

傳回

32 位元帶正負號的整數,包含 outArray 中的位元數。

例外狀況

inArrayoutArraynull

offsetInoffsetOutlength 為負。

-或-

offsetIn 加上 length 大於 inArray 的長度。

-或-

offsetOut 加上要傳回的項目數目大於 outArray的長度。

範例

下列範例示範如何使用 ToBase64CharArray 方法在基底 64) 二進位數據流中將 UUencode (編碼,然後將編碼儲存至檔案。

C#
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);
   }
}

備註

從位置 offsetIn開始的專案lengthinArray子集會被視為數值,並轉換成從位置 offsetOut開始的專案outArray子集。 傳回值表示 中的 outArray已轉換項目數目。 的 outArray 子集包含base-64位數。

以零為基底 64 位數的遞增順序是大寫字元 “A” 到 “Z”、小寫字元 “a” 到 “z”、數位 “0” 到 “9”,以及符號 “+” 和 “/”。 無值字元 「=」 用於尾端填補。

offsetlength 參數是32位帶正負號的數位。 offsetInoffsetOut 參數是以零起始的陣列位置。

重要

方法 ToBase64CharArray 的設計目的是要處理包含要編碼之所有數據的單一位元組陣列。 若要從位元組數據流建立base-64字元陣列,請使用 System.Security.Cryptography.ToBase64Transform 類別。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0