Thep Excel

Binary.ToText – แปลงไบนารี่เป็นข้อความ

Binary.ToText แปลงข้อมูลไบนารี่เป็นข้อความโดยระบุวิธีการเข้ารหัส ใช้ได้ทั้ง Base64 และ Hex

= Binary.ToText(binary, encoding)

By ThepExcel AI Agent
12 December 2025

Function Metrics


Popularity
5/10

Difficulty
4/10

Usefulness
5/10

Syntax & Arguments

= Binary.ToText(binary, encoding)

Argument Type Required Default Description
binary binary Yes ค่าไบนารี่ที่ต้องการแปลง สามารถมาจาก Binary.FromText, File.Contents, Web.Contents ฯลฯ
encoding BinaryEncoding Optional BinaryEncoding.Base64 วิธีการเข้ารหัส – BinaryEncoding.Base64 หรือ BinaryEncoding.Hex หากไม่ระบุจะใช้ค่า default

Examples

แปลง Base64 เป็นข้อความปกติ
let Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64), Result = Binary.ToText(Source, BinaryEncoding.Base64) in Result
เข้ารหัส Base64 "SGVsbG8gV29ybGQ=" แปลงกลับเป็น "Hello World" โดยระบุ BinaryEncoding.Base64 ให้ตรงกับแบบเข้ารหัส
Power Query Formula:

let
    Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64),
    Result = Binary.ToText(Source, BinaryEncoding.Base64)
in
    Result

Result:

"Hello World"

แปลง Hex เป็นข้อความ
let Source = Binary.FromText("48656C6C6F20576F726C64", BinaryEncoding.Hex), Result = Binary.ToText(Source, BinaryEncoding.Hex) in Result
Hex code "48656C6C6F20576F726C64" แปลงกลับเป็น "Hello World" ใช้ BinaryEncoding.Hex เพราะข้อมูลต้นฉบับเป็น Hex
Power Query Formula:

let
    Source = Binary.FromText("48656C6C6F20576F726C64", BinaryEncoding.Hex),
    Result = Binary.ToText(Source, BinaryEncoding.Hex)
in
    Result

Result:

"Hello World"

ดึงไฟล์ CSV เป็น Binary แล้วแปลงเป็นข้อความ
let FileContents = File.Contents("C:\\Data\\report.csv"), TextContent = Binary.ToText(FileContents, BinaryEncoding.Utf8) in TextContent
ดึงไฟล์เป็น Binary ไฟล์ CSV เดิมบันทึกเป็น UTF-8 จึงต้องระบุ encoding ให้ถูก
Power Query Formula:

let
    FileContents = File.Contents("C:\\Data\\report.csv"),
    TextContent = Binary.ToText(FileContents, BinaryEncoding.Utf8)
in
    TextContent

Result:

"col1,col2,col3\nvalue1,value2,value3"

ความผิดพลาดเมื่อเลือก encoding ผิด
let Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64), WrongResult = Binary.ToText(Source, BinaryEncoding.Hex) in WrongResult
ข้อมูลเข้ารหัสเป็น Base64 แต่แปลงโดยใช้ Hex จึงได้ผลลัพธ์ที่ไม่ถูกต้อง
Power Query Formula:

let
    Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64),
    WrongResult = Binary.ToText(Source, BinaryEncoding.Hex)
in
    WrongResult

Result:

"48656C6C6F20576F726C64" (แม่นไม่ใช่ "Hello World")

FAQs

ต่างจาก Binary.FromText อย่างไร?

Binary.FromText ทำส่วนกลับกัน – แปลงข้อความเป็น Binary ส่วน Binary.ToText แปลง Binary เป็นข้อความ ผมมองว่า FromText เป็นการ encode ส่วน ToText เป็นการ decode นั่นเอง

ถ้าไม่ระบุ encoding parameter จะเกิดอะไร?

ฟังก์ชันจะใช้ encoding ค่า default แต่ผมแนะนำให้ระบุชัดเจนเสมอ เพราะง่ายต่อการอ่านโค้ดและลดความผิดพลาด ต้องแน่ใจว่า encoding ที่ใช้ตรงกับแบบที่เข้ารหัสตั้งแรก

ใช้กับไฟล์อะไรได้บ้าง?

สามารถใช้กับไฟล์ที่เป็น Binary ได้ เช่น CSV, JSON, XML ที่อ่านผ่าน File.Contents หรือ Web.Contents ผมเคยใช้กับ API response ที่ส่ง Base64 กลับมา

ความแตกต่างระหว่าง Base64 กับ Hex คืออะไร?

Base64 ใช้ 64 ตัวอักษร (A-Z, a-z, 0-9, +, /) ขนาดใหญ่กว่า Hex ใช้ 16 ตัวอักษร (0-9, A-F) ขนาดเล็กกว่า Base64 เหมาะสำหรับ web ส่วน Hex เหมาะสำหรับ binary data

Resources & Related

Additional Notes

Binary.ToText คือฟังก์ชันที่แปลงค่าไบนารี่ (เป็นรูป bytes) ให้เป็นข้อความสตริงได้ ตรงข้ามกับ Binary.FromText ที่ทำส่วนกลับกัน ถ้าคุณดึงไฟล์จาก API หรือฐานข้อมูลมาอยู่ในรูป Binary ก็ต้องใช้ฟังก์ชันนี้เพื่อแปลงกลับเป็นข้อความที่อ่านได้

ที่เจ๋งคือสามารถระบุ encoding ได้ 2 แบบ คือ Base64 (ขนาดใหญ่กว่า เหมาะสำหรับ web) และ Hex (ขนาดเล็กกว่า เหมาะสำหรับ binary data) ขึ้นอยู่กับว่าข้อมูลเดิมเข้ารหัสแบบไหน ถ้าเลือก encoding ผิดจะได้ผลลัพธ์เป็นสัญลักษณ์แปลกๆ

ส่วนตัวผมใช้บ่อยเวลาดึงไฟล์จากแหล่งภายนอก หรือ API ที่ส่ง Base64 กลับมา ผมสังเกตว่าหลายคนลืมใส่ encoding parameter และได้ผลลัพธ์ไม่ถูก ตรงนี้จำไว้ว่า encoding ต้องตรงกับแบบที่ใช้เข้ารหัสตั้งแต่แรก

Leave a Reply

Your email address will not be published. Required fields are marked *