---
title: Binary.Length – ได้ความยาว Binary
url: https://www.thepexcel.com/functions/power-query/binary-functions/binary-length/
type: function-explainer
program: Power Query
syntax: Binary.Length(binary as nullable binary) as nullable number
date: 2025-12-04
updated: 2025-12-17
scores:
  popularity: 4
  difficulty: 3
  usefulness: 4
---

# Binary.Length – ได้ความยาว Binary

> ตรวจสอบจำนวน Bytes ในค่า Binary

## คำอธิบาย

Binary.Length ตรวจสอบและคืนค่าจำนวน Bytes ในค่า Binary เป็นตัวเลข เช่น Binary ที่มี 5 Bytes จะคืนค่า 5

## Syntax

```excel
Binary.Length(binary as nullable binary) as nullable number
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| binary | Yes | binary |  | ค่า Binary (nullable) ที่ต้องการตรวจสอบความยาว (จำนวน Bytes) |

## เคสการใช้งาน

### ตรวจสอบขนาดไฟล์ Binary

ใช้ Binary.Length เพื่อตรวจสอบว่า Binary มีขนาดเป็นจำนวน Bytes เท่าไร ประโยชน์สำหรับตรวจสอบว่าไฟล์สมบูรณ์

### ตรวจสอบความถูกต้องของข้อมูล

เช่น หากคาดว่า Binary ควรมีความยาว 256 Bytes แต่จริงๆได้ 255 Bytes แสดงว่าข้อมูลบกพร่อง

### จัดกระทำข้อมูล Binary ตามขนาด

ใช้ Binary.Length ตัดสินใจว่าจะแบ่งหรือประมวลผล Binary ด้วยวิธีไหน

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ตรวจสอบความยาวของ Binary ง่ายๆ

```excel
Binary.Length(Binary.FromList({0, 1, 2, 3, 4}))
```

**ผลลัพธ์:** `5`

สร้าง Binary จากรายการตัวเลข {0, 1, 2, 3, 4} ซึ่งมี 5 ตัว ใช้ Binary.Length ตรวจสอบความยาว ได้ผลลัพธ์ 5

### 2. ตัวอย่างที่ 2: ตรวจสอบความยาวข้อความแปลงเป็น Binary

```excel
let
    Text = "Hello World",
    BinaryData = Binary.FromText(Text),
    Length = Binary.Length(BinaryData)
in
    Length
```

**ผลลัพธ์:** `11`

แปลงข้อความ "Hello World" เป็น Binary (11 ตัวอักษร: H,e,l,l,o,space,W,o,r,l,d) ใช้ Binary.Length ได้ผลลัพธ์ 11

### 3. ตัวอย่างที่ 3: ตรวจสอบความยาว Binary ว่างและ Binary ที่รวมกัน

```excel
let
    EmptyBinary = Binary.FromList({}),
    LengthEmpty = Binary.Length(EmptyBinary),
    Part1 = Binary.FromList({65, 66}),
    Part2 = Binary.FromList({67, 68}),
    Combined = Binary.Combine({Part1, Part2}),
    LengthCombined = Binary.Length(Combined)
in
    [EmptyLength = LengthEmpty, CombinedLength = LengthCombined]
```

**ผลลัพธ์:** `[EmptyLength = 0, CombinedLength = 4]`

สร้าง Binary ว่าง ตรวจสอบความยาว ได้ 0 จากนั้นรวม 2 Binary (Part1: 2 Bytes, Part2: 2 Bytes) เข้าเป็น Binary เดียว (4 Bytes รวม) ตรวจสอบความยาวของ Binary ที่รวมกันได้ 4

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

**Q: Binary.Length กับ Text.Length ต่างกันอย่างไร?**

Binary.Length ตรวจสอบจำนวน Bytes ในข้อมูล Binary ส่วน Text.Length ตรวจสอบจำนวนตัวอักษรในข้อความ สำหรับข้อความภาษาอังกฤษ 1 ตัวอักษร = 1 Byte แต่ภาษาอื่นอาจต่างกัน

**Q: ถ้า Input เป็น null จะได้อะไร?**

Binary.Length(null) จะคืนค่า null ตามหลักการ null propagation ของ Power Query

**Q: ความยาว Binary สูงสุดเท่าไร?**

ไม่มีขีดจำกัดในเรื่องความยาว Binary แต่ขึ้นอยู่กับหน่วยความจำที่มีอยู่ใน Power Query

**Q: ใช้ Binary.Length ได้กับการตรวจสอบรูปแบบไฟล์หรือไม่?**

ได้บางส่วน เช่น ตรวจสอบว่า Binary มีความยาวตามลักษณะเฉพาะของไฟล์ (Image header ควรมีความยาวเท่ากัน) แต่การตรวจสอบรูปแบบต้องตรวจสอบเนื้อหา (Signature bytes) ไม่ใช่แค่ความยาว

## ฟังก์ชันที่เกี่ยวข้อง

- [Binary.Buffer – สร้างค่า Binary ที่เสถียร](https://www.thepexcel.com/functions/power-query/binary-functions/binary-buffer/)
- [Binary.FromList – สร้างข้อมูลไบนารี่จากรายการ](https://www.thepexcel.com/functions/power-query/binary-functions/binary-fromlist/)
- [Binary.FromText – แปลงข้อความเป็นข้อมูลไบนารี่](https://www.thepexcel.com/functions/power-query/binary-functions/binary-fromtext/)
- [Binary.Range – เลือกช่วงข้อมูลไบนารี่](https://www.thepexcel.com/functions/power-query/binary-functions/binary-range/)
- [Text.Length – นับจำนวนตัวอักษรในข้อความ](https://www.thepexcel.com/functions/power-query/text-functions/text-length/)

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

- [Microsoft Learn: Binary.Length](https://learn.microsoft.com/en-us/powerquery-m/binary-length) _(Official Documentation)_
- [Power Query M Language - Binary Functions](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-function-reference#binary) _(Function Reference)_

---

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