---
title: Binary.FromText – แปลงข้อความเป็นข้อมูลไบนารี่
url: https://www.thepexcel.com/functions/power-query/binary-functions/binary-fromtext/
type: function-explainer
program: Power Query
syntax: "= Binary.FromText(text, encoding)"
date: 2025-12-12
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 5
---

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

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

## คำอธิบาย

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

## Syntax

```excel
= Binary.FromText(text, encoding)
```

## Arguments

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

## ตัวอย่าง

### 1. แปลง Base64 เป็นไบนารี่

```excel
= Binary.FromText("SGVsbG8gV29ybGQ=")
```

**ผลลัพธ์:** `ข้อมูลไบนารี่ที่แปลงมาจาก Base64`

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

### 2. แปลง Hex เป็นไบนารี่

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

**ผลลัพธ์:** `ข้อมูลไบนารี่จากการเข้ารหัส Hex`

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

### 3. ใช้ในการดึงข้อมูลรูปภาพจาก API

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

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

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

### 4. สร้าง Custom Function สำหรับแปลงหลายรูปแบบ

```excel
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")
```

**ผลลัพธ์:** `ไบนารี่ที่แปลงจาก Hex`

สร้าง function ที่สามารถรับพารามิเตอร์ประเภทการเข้ารหัสและแปลงไปตามนั้น ประโยชน์ใหญ่เวลาต้องจัดการหลายรูปแบบ

## หมายเหตุเพิ่มเติม

- ผมแนะนำให้ใช้ Binary.FromText เมื่อข้อมูลจาก API มาในรูป Base64 string โดยเฉพาะเวลาต้องเก็บเป็นไฟล์

- ถ้าข้อความเป็น Plain Text ธรรมดา ให้ใช้ Text.ToBinary แทน ไม่ใช่ Binary.FromText

- ส่วนตัวผมเชื่อว่า Base64 นั้นดีกว่า Hex สำหรับการส่งข้อมูลทั่วไป เพราะให้ขนาดไฟล์เล็กกว่า

- ใช้ร่วมกับ File.WriteAllBytes เมื่อต้องบันทึกข้อมูลไบนารี่เป็นไฟล์ได้สวยๆ

## คำถามที่พบบ่อย

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

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

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

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

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

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

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Binary.FromText](https://learn.microsoft.com/en-us/powerquery-m/binary-fromtext) _(official)_
- [Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/binary-functions/binary-fromtext/](https://www.thepexcel.com/functions/power-query/binary-functions/binary-fromtext/)_
