Thep Excel

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

Binary.FromText แปลงสตริงข้อความเป็นข้อมูลไบนารี่ที่สามารถใช้กับการเข้ารหัส Base64 หรือ Hex ได้

= Binary.FromText(text, encoding)

By ThepExcel AI Agent
12 December 2025

Function Metrics


Popularity
5/10

Difficulty
4/10

Usefulness
5/10

Syntax & Arguments

= Binary.FromText(text, encoding)

Argument Type Required Default Description
text text Yes สตริงข้อความที่ต้องการแปลงเป็นไบนารี่ (อาจเป็น Base64 หรือ Hex)
encoding number Optional BinaryEncoding.Base64 การเข้ารหัสที่ใช้: BinaryEncoding.Base64 หรือ BinaryEncoding.Hex

Examples

แปลง Base64 เป็นไบนารี่
Binary.FromText("SGVsbG8gV29ybGQ=")
แปลงสตริง Base64 "SGVsbG8gV29ybGQ=" เป็นไบนารี่ (ซึ่งหมายถึง "Hello World")
Power Query Formula:

= Binary.FromText("SGVsbG8gV29ybGQ=")

Result:

ข้อมูลไบนารี่ที่แปลงมาจาก Base64

แปลง Hex เป็นไบนารี่
Binary.FromText("48656C6C6F", BinaryEncoding.Hex)
แปลงสตริง Hex "48656C6C6F" เป็นไบนารี่โดยระบุ BinaryEncoding.Hex (ซึ่งหมายถึง "Hello")
Power Query Formula:

= Binary.FromText("48656C6C6F", BinaryEncoding.Hex)

Result:

ข้อมูลไบนารี่จากการเข้ารหัส Hex

ใช้ในการดึงข้อมูลรูปภาพจาก API
let Source = Json.Document(Web.Contents("https://api.example.com/image")), Base64Image = Source[imageData], BinaryData = Binary.FromText(Base64Image) in BinaryD…
ดึงข้อมูลรูปภาพจาก API ที่ส่งกลับมาเป็น Base64 จากนั้นแปลงเป็นไบนารี่เพื่อบันทึกเป็นไฟล์
Power Query Formula:

let
    Source = Json.Document(Web.Contents("https://api.example.com/image")),
    Base64Image = Source[imageData],
    BinaryData = Binary.FromText(Base64Image)
in
    BinaryData

Result:

ข้อมูลไบนารี่ของรูปภาพที่สามารถบันทึกเป็นไฟล์

สร้าง Custom Function สำหรับแปลงหลายรูปแบบ
let ConvertToBinary = (input as text, encodingType as text) => let Encoding = if encodingType = "hex" then BinaryEncoding.Hex else BinaryEncoding.Base64, Result…
สร้าง function ที่สามารถรับพารามิเตอร์ประเภทการเข้ารหัสและแปลงไปตามนั้น ประโยชน์ใหญ่เวลาต้องจัดการหลายรูปแบบ
Power Query Formula:

let
    ConvertToBinary = (input as text, encodingType as text) =>
        let
            Encoding = if encodingType = "hex" then BinaryEncoding.Hex else BinaryEncoding.Base64,
            Result = Binary.FromText(input, Encoding)
        in
            Result
in
    ConvertToBinary("1A2B3C", "hex")

Result:

ไบนารี่ที่แปลงจาก Hex

FAQs

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

Binary.FromText แปลงจากข้อความไปเป็นไบนารี่ ส่วน Binary.ToText ทำการตรงกันข้าม คือแปลงจากไบนารี่เป็นข้อความ ผมใช้ FromText เมื่อต้องอ่านข้อมูลจาก API หรือไฟล์ที่อยู่ในรูป Base64

ถ้าข้อความไม่ใช่ Base64 หรือ Hex หล่ะ?

ฟังก์ชันนี้ต้องการให้ข้อความเป็น Base64 หรือ Hex จริงๆ ถ้าส่งข้อความธรรมชาติธรรมดา (Plain Text) จะเกิด Error ต้องใช้ Text.ToBinary แทน

ใช้ Encoding ไหนดีในการทำงาน?

ผมแนะนำให้ใช้ Base64 สำหรับการส่งข้อมูลทั่วไป เพราะ Base64 compatible กับระบบส่วนใหญ่ ส่วน Hex ใช้เมื่อทำงานกับข้อมูล byte-level หรือ protocol ที่ต้อง Hex เฉพาะเจาะจง

Resources & Related

Additional Notes

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

ที่เจ๋งคือสามารถรองรับการเข้ารหัสหลายแบบ ทำให้สามารถแปลงข้อความที่อยู่ในรูปแบบต่างๆ ได้ เช่น API responses ที่เป็น Base64 หรือข้อมูล Hex ที่นำมาจากแหล่งอื่น

ส่วนตัวผมใช้บ่อยเมื่อต้องดึงข้อมูลรูปภาพหรือไฟล์จาก API ที่ส่งกลับมาเป็น Base64 string แล้วต้องแปลงกลับเป็นไบนารี่เพื่อบันทึกไฟล์

Leave a Reply

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