Text.ToBinary

วากยสัมพันธ์

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

ประมาณ

เข้ารหัสค่าข้อความเป็นค่าไบนารีโดยใช้การเข้ารหัสที่ระบุ

  • text: ข้อความที่จะเข้ารหัส
  • encoding: (ไม่บังคับ) การเข้ารหัสที่ใช้ในการแปลงข้อความเป็นไบนารี ใช้เพื่อ BinaryEncoding.Type ระบุประเภทของการเข้ารหัส ถ้าไม่ได้ระบุค่านี้ ค่าเริ่มต้น BinaryEncoding.Utf8คือ
  • includeByteOrderMark: (ไม่บังคับ) กําหนดว่าควรรวมเครื่องหมายลําดับไบต์ (BOM) ไว้ที่จุดเริ่มต้นของเอาต์พุตไบนารีหรือไม่ ตั้งค่าเป็น true รวม BOM โดยอัตโนมัติ มิฉะนั้นfalse ถ้าไม่ได้ระบุค่านี้ ค่าเริ่มต้น falseคือ

ตัวอย่างที่ 1

เข้ารหัสข้อความเป็นไบนารี สร้างสตริง Base64 ที่ดูได้ จากนั้นถอดรหัสกลับเป็นข้อความ

การใช้งาน

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",
    BinaryBase64 = "VGVzdGluZyAxLTItMw==",
    DecodedText = "Testing 1-2-3"
]

ตัวอย่างที่ 2

เข้ารหัสข้อความเป็นไบนารีด้วยเครื่องหมายลําดับไบต์ (BOM) สร้างสตริงเลขฐานสิบหกที่ดูได้ แล้วถอดรหัสกลับเป็นข้อความ

การใช้งาน

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",
    BinaryHex = "fffe540065007300740069006e006700200031002d0032002d003300",
    DecodedText = "Testing 1-2-3",
    IsIdentical = true
]