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

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

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

## คำอธิบาย

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

## Syntax

```excel
= Binary.ToText(binary, encoding)
```

## Arguments

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

## ตัวอย่าง

### 1. แปลง Base64 เป็นข้อความปกติ

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

**ผลลัพธ์:** `"Hello World"`

เข้ารหัส Base64 "SGVsbG8gV29ybGQ=" แปลงกลับเป็น "Hello World" โดยระบุ BinaryEncoding.Base64 ให้ตรงกับแบบเข้ารหัส

### 2. แปลง Hex เป็นข้อความ

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

**ผลลัพธ์:** `"Hello World"`

Hex code "48656C6C6F20576F726C64" แปลงกลับเป็น "Hello World" ใช้ BinaryEncoding.Hex เพราะข้อมูลต้นฉบับเป็น Hex

### 3. ดึงไฟล์ CSV เป็น Binary แล้วแปลงเป็นข้อความ

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

**ผลลัพธ์:** `"col1,col2,col3\nvalue1,value2,value3"`

ดึงไฟล์เป็น Binary ไฟล์ CSV เดิมบันทึกเป็น UTF-8 จึงต้องระบุ encoding ให้ถูก

### 4. ความผิดพลาดเมื่อเลือก encoding ผิด

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

**ผลลัพธ์:** `"48656C6C6F20576F726C64" (แม่นไม่ใช่ "Hello World")`

ข้อมูลเข้ารหัสเป็น Base64 แต่แปลงโดยใช้ Hex จึงได้ผลลัพธ์ที่ไม่ถูกต้อง

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

- ผมแนะนำให้ระบุ encoding ชัดเจนเสมอ แม้ว่าจะมี default ก็ตาม เพราะเวลาอ่านโค้ดขึ้นมากว่าเดือน ช่วยอธิบายว่าเลือก encoding แบบไหนและทำไม

- ถ้าได้ผลลัพธ์เป็นสัญลักษณ์แปลกๆ หรือรูปแบบไม่เข้าใจ ให้ลองสลับระหว่าง BinaryEncoding.Base64 และ BinaryEncoding.Hex ตรวจสอบว่าข้อมูล source เข้ารหัสแบบไหน

- ส่วนตัวผมชอบใช้ Binary.ToText ร่วมกับ Combiner.CombineTextByDelimiter เพื่อแยก binary data เป็นหลายส่วน

- เวลาทำงานกับ API ที่ส่ง binary data กลับมา ให้ตรวจสอบ documentation ของ API ว่าใช้ encoding แบบไหน ถ้าไม่ชัดเจนให้ลองทั้ง 2 แบบ

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

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

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

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

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

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

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

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

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

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

- [Microsoft Learn: Binary.ToText](https://learn.microsoft.com/en-us/powerquery-m/binary-totext) _(official)_
- [Microsoft Learn: Binary.FromText](https://learn.microsoft.com/en-us/powerquery-m/binary-fromtext) _(official)_
- [Microsoft Learn: BinaryEncoding Type](https://learn.microsoft.com/en-us/powerquery-m/binaryencoding-type) _(official)_

---

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