Partager via


Text.ToBinary

Syntaxe

Text.ToBinary(
    text as nullable text,
    optional encoding as nullable number,
    optional includeByteOrderMark as nullable logical
) as nullable binary

À propos

Encode une valeur de texte dans une valeur binaire à l’aide de l’encodage spécifié.

  • text: texte à encoder.
  • encoding: (Facultatif) Encodage utilisé pour convertir le texte en binaire. Permet BinaryEncoding.Type de spécifier le type d’encodage. Si cette valeur n’est pas spécifiée, la valeur par défaut est BinaryEncoding.Utf8.
  • includeByteOrderMark: (Facultatif) Détermine si une marque d’ordre d’octet (BOM) doit être incluse au début de la sortie binaire. Réglez sur true pour inclure automatiquement le BOM, sinon false. Si cette valeur n’est pas spécifiée, la valeur par défaut est false.

Exemple 1

Encodez du texte en binaire, produisez une chaîne Base64 visible, puis décodez-la en texte.

Utilisation

let
    originalText = "Testing 1-2-3",

    // Default UTF-8 binary
    binaryData = Text.ToBinary(originalText),

    // Convert binary to viewable Base64 string
    encodedText = Binary.ToText(binaryData, BinaryEncoding.Base64),

    // Decode back to text
    decodedText = Text.FromBinary(binaryData),

    result = [
        OriginalText = originalText,
        BinaryBase64 = encodedText,
        DecodedText = decodedText
    ]
in
    result

Output

[
    OriginalText = "Testing 1-2-3",
    BinaryEncoded = "VGVzdGluZyAxLTItMw==",
    DecodedText = "Testing 1-2-3"
]

Exemple 2

Encodez du texte en binaire avec une marque d’ordre d’octet (BOM), produisez une chaîne hexadécimale visible, puis décodez-la en texte.

Utilisation

let
    originalText = "Testing 1-2-3",

    // Convert to binary with BOM
    binaryData = Text.ToBinary(originalText, TextEncoding.Utf16, true),

    // Show binary as hex to demonstrate presence of BOM (fffe)
    binaryAsHex = Binary.ToText(binaryData, BinaryEncoding.Hex),

    // Decode back to text
    decodedText = Text.FromBinary(binaryData, TextEncoding.Utf16),

    // Compare original text and decoded text
    isIdentical = originalText = decodedText,

    result = [
        OriginalText = originalText,
        BinaryHex = binaryAsHex,
        DecodedText = decodedText,
        IsIdentical = isIdentical
    ]
in
    result

Output

[
    OriginalText = "Testing 1-2-3", 
    DecodedText = "fffe540065007300740069006e006700200031002d0032002d003300",
    DecodedText = "Testing 1-2-3", 
    IsIdentical = true 
]