Base64FormattingOptions Enumeration
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt an, ob relevante ToBase64CharArray und ToBase64String Methoden Zeilenumbrüche in ihre Ausgabe einfügen.
Diese Enumeration unterstützt eine bitweise Kombination ihrer Memberwerte.
public enum class Base64FormattingOptions
[System.Flags]
public enum Base64FormattingOptions
[<System.Flags>]
type Base64FormattingOptions =
Public Enum Base64FormattingOptions
- Vererbung
- Attribute
Felder
| Name | Wert | Beschreibung |
|---|---|---|
| None | 0 | Fügt zeilenumbrüche nicht nach allen 76 Zeichen in der Zeichenfolgendarstellung ein. |
| InsertLineBreaks | 1 | Fügt Zeilenumbrüche nach allen 76 Zeichen in der Zeichenfolgendarstellung ein. |
Beispiele
Im folgenden Beispiel wird die Convert.ToBase64String(Byte[], Base64FormattingOptions) Methode mit einem InsertLineBreaks Argument aufgerufen, um Zeilenumbrüche in die Zeichenfolge einzufügen, die durch codieren eines Bytearrays mit 100 Elementen erzeugt wird:
using System;
public class Example
{
public static void Main()
{
// Define a byte array.
var bytes = new byte[100];
int originalTotal = 0;
for (int ctr = 0; ctr <= bytes.GetUpperBound(0); ctr++) {
bytes[ctr] = (byte)(ctr + 1);
originalTotal += bytes[ctr];
}
// Display summary information about the array.
Console.WriteLine("The original byte array:");
Console.WriteLine(" Total elements: {0}", bytes.Length);
Console.WriteLine(" Length of String Representation: {0}",
BitConverter.ToString(bytes).Length);
Console.WriteLine(" Sum of elements: {0:N0}", originalTotal);
Console.WriteLine();
// Convert the array to a base 64 string.
string s = Convert.ToBase64String(bytes,
Base64FormattingOptions.InsertLineBreaks);
Console.WriteLine("The base 64 string:\n {0}\n", s);
// Restore the byte array.
Byte[] newBytes = Convert.FromBase64String(s);
int newTotal = 0;
foreach (var newByte in newBytes)
newTotal += newByte;
// Display summary information about the restored array.
Console.WriteLine(" Total elements: {0}", newBytes.Length);
Console.WriteLine(" Length of String Representation: {0}",
BitConverter.ToString(newBytes).Length);
Console.WriteLine(" Sum of elements: {0:N0}", newTotal);
}
}
// The example displays the following output:
// The original byte array:
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
//
// The base 64 string:
// AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
// Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
//
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
open System
// Define a byte array.
let bytes =
[| for i = 0 to 99 do byte (i + 1) |]
let originalTotal = Array.sumBy int bytes
// Display summary information about the array.
printfn "The original byte array:"
printfn $" Total elements: {bytes.Length}"
printfn $" Length of String Representation: {BitConverter.ToString(bytes).Length}"
printfn $" Sum of elements: {originalTotal:N0}"
printfn ""
// Convert the array to a base 64 string.
let s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks)
printfn $"The base 64 string:\n {s}\n"
// Restore the byte array.
let newBytes = Convert.FromBase64String s
let newTotal = Array.sumBy int newBytes
// Display summary information about the restored array.
printfn $" Total elements: {newBytes.Length}"
printfn $" Length of String Representation: {BitConverter.ToString(newBytes).Length}"
printfn $" Sum of elements: {newTotal:N0}"
// The example displays the following output:
// The original byte array:
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
//
// The base 64 string:
// AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
// Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
//
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
Module Example
Public Sub Main()
' Define a byte array.
Dim bytes(99) As Byte
Dim originalTotal As Integer = 0
For ctr As Integer = 0 To bytes.GetUpperBound(0)
bytes(ctr) = CByte(ctr + 1)
originalTotal += bytes(ctr)
Next
' Display summary information about the array.
Console.WriteLine("The original byte array:")
Console.WriteLine(" Total elements: {0}", bytes.Length)
Console.WriteLine(" Length of String Representation: {0}",
BitConverter.ToString(bytes).Length)
Console.WriteLine(" Sum of elements: {0:N0}", originalTotal)
Console.WriteLine()
' Convert the array to a base 64 string.
Dim s As String = Convert.ToBase64String(bytes,
Base64FormattingOptions.InsertLineBreaks)
Console.WriteLine("The base 64 string:{1} {0}{1}",
s, vbCrLf)
' Restore the byte array.
Dim newBytes() As Byte = Convert.FromBase64String(s)
Dim newTotal As Integer = 0
For Each newByte In newBytes
newTotal += newByte
Next
' Display summary information about the restored array.
Console.WriteLine(" Total elements: {0}", newBytes.Length)
Console.WriteLine(" Length of String Representation: {0}",
BitConverter.ToString(newBytes).Length)
Console.WriteLine(" Sum of elements: {0:N0}", newTotal)
End Sub
End Module
' The example displays the following output:
' The original byte array:
' Total elements: 100
' Length of String Representation: 299
' Sum of elements: 5,050
'
' The base 64 string:
' AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
' Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
'
' Total elements: 100
' Length of String Representation: 299
' Sum of elements: 5,050
Wie die Ausgabe des Beispiels zeigt, kann das Convert.FromBase64String ursprüngliche Bytearray wiederhergestellt werden. Die Zeilenumbruchzeichen werden während der Konvertierung ignoriert.
Hinweise
Die Convert.ToBase64CharArray Methoden Convert.ToBase64String konvertieren den Wert eines Arrays von 8-Bit-ganzzahlen ohne Vorzeichen in eine entsprechende Zeichenfolgendarstellung, die aus Basis64 Ziffern besteht. Die Zeichenfolgendarstellung kann einen oder mehrere Zeilenumbrüche enthalten, wobei ein Zeilenumbruch als Wagenrücklaufzeichen (U+000D) definiert ist, gefolgt von einem Zeilenvorschubzeichen (U+000A). Da Zeilenumbrüche als Leerzeichen in einer Base64-Codierung gelten, werden sie beim Konvertieren einer base64-codierten Zeichenfolge zurück in ein Bytearray ignoriert. Die Zeilenumbrüche sind einfach praktisch, wenn die codierte Zeichenfolge auf einem Steuerelement oder einem Gerät wie einem Konsolenfenster angezeigt wird.
Die None Werte InsertLineBreaks schließen sich gegenseitig aus. Obwohl die Base64FormattingOptions Enumeration mit dem FlagsAttribute Attribut gekennzeichnet ist, ist es nicht sinnvoll, eine bitweise Kombination dieser beiden Werte auszuführen.