Binary.ToText แปลงข้อมูลไบนารี่เป็นข้อความโดยระบุวิธีการเข้ารหัส ใช้ได้ทั้ง Base64 และ Hex
= Binary.ToText(binary, encoding)
= 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 |
let Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64), Result = Binary.ToText(Source, BinaryEncoding.Base64) in Resultlet
Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64),
Result = Binary.ToText(Source, BinaryEncoding.Base64)
in
Result
"Hello World"
let Source = Binary.FromText("48656C6C6F20576F726C64", BinaryEncoding.Hex), Result = Binary.ToText(Source, BinaryEncoding.Hex) in Resultlet
Source = Binary.FromText("48656C6C6F20576F726C64", BinaryEncoding.Hex),
Result = Binary.ToText(Source, BinaryEncoding.Hex)
in
Result
"Hello World"
let FileContents = File.Contents("C:\\Data\\report.csv"), TextContent = Binary.ToText(FileContents, BinaryEncoding.Utf8) in TextContentlet
FileContents = File.Contents("C:\\Data\\report.csv"),
TextContent = Binary.ToText(FileContents, BinaryEncoding.Utf8)
in
TextContent
"col1,col2,col3\nvalue1,value2,value3"
let Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64), WrongResult = Binary.ToText(Source, BinaryEncoding.Hex) in WrongResultlet
Source = Binary.FromText("SGVsbG8gV29ybGQ=", BinaryEncoding.Base64),
WrongResult = Binary.ToText(Source, BinaryEncoding.Hex)
in
WrongResult
"48656C6C6F20576F726C64" (แม่นไม่ใช่ "Hello World")
Binary.FromText ทำส่วนกลับกัน – แปลงข้อความเป็น Binary ส่วน Binary.ToText แปลง Binary เป็นข้อความ ผมมองว่า FromText เป็นการ encode ส่วน ToText เป็นการ decode นั่นเอง
ฟังก์ชันจะใช้ encoding ค่า default แต่ผมแนะนำให้ระบุชัดเจนเสมอ เพราะง่ายต่อการอ่านโค้ดและลดความผิดพลาด ต้องแน่ใจว่า encoding ที่ใช้ตรงกับแบบที่เข้ารหัสตั้งแรก
สามารถใช้กับไฟล์ที่เป็น Binary ได้ เช่น CSV, JSON, XML ที่อ่านผ่าน File.Contents หรือ Web.Contents ผมเคยใช้กับ API response ที่ส่ง Base64 กลับมา
Base64 ใช้ 64 ตัวอักษร (A-Z, a-z, 0-9, +, /) ขนาดใหญ่กว่า Hex ใช้ 16 ตัวอักษร (0-9, A-F) ขนาดเล็กกว่า Base64 เหมาะสำหรับ web ส่วน Hex เหมาะสำหรับ binary data
Binary.ToText คือฟังก์ชันที่แปลงค่าไบนารี่ (เป็นรูป bytes) ให้เป็นข้อความสตริงได้ ตรงข้ามกับ Binary.FromText ที่ทำส่วนกลับกัน ถ้าคุณดึงไฟล์จาก API หรือฐานข้อมูลมาอยู่ในรูป Binary ก็ต้องใช้ฟังก์ชันนี้เพื่อแปลงกลับเป็นข้อความที่อ่านได้
ที่เจ๋งคือสามารถระบุ encoding ได้ 2 แบบ คือ Base64 (ขนาดใหญ่กว่า เหมาะสำหรับ web) และ Hex (ขนาดเล็กกว่า เหมาะสำหรับ binary data) ขึ้นอยู่กับว่าข้อมูลเดิมเข้ารหัสแบบไหน ถ้าเลือก encoding ผิดจะได้ผลลัพธ์เป็นสัญลักษณ์แปลกๆ
ส่วนตัวผมใช้บ่อยเวลาดึงไฟล์จากแหล่งภายนอก หรือ API ที่ส่ง Base64 กลับมา ผมสังเกตว่าหลายคนลืมใส่ encoding parameter และได้ผลลัพธ์ไม่ถูก ตรงนี้จำไว้ว่า encoding ต้องตรงกับแบบที่ใช้เข้ารหัสตั้งแต่แรก