Text.ToBinary

Syntax

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

Om

Kodar ett textvärde till ett binärt värde med den angivna kodningen.

  • text: Texten som ska kodas.
  • encoding: (Valfritt) Kodningen som används för att konvertera texten till binär. Använd BinaryEncoding.Type för att ange typ av kodning. Om det här värdet inte anges är BinaryEncoding.Utf8standardvärdet .
  • includeByteOrderMark: (Valfritt) Avgör om ett byteordningsmärke (BOM) ska inkluderas i början av de binära utdata. Ange true för att automatiskt inkludera BOM ("Byte Order Mark"), annars false. Om det här värdet inte anges är falsestandardvärdet .

Exempel 1

Koda text till binär, skapa en synlig Base64-sträng och avkoda den sedan tillbaka till text.

Användning

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

Resultat

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

Exempel 2

Koda text till binär med en Byte Order Mark (BOM), skapa en synlig hexadecimal sträng och avkoda den sedan tillbaka till text.

Användning

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

Resultat

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