Hexadecimal คืออะไร? คู่มือ Hex สำหรับนักพัฒนา
Hexadecimal ปรากฏอยู่แทบทุกที่ในการเขียนโปรแกรม ตั้งแต่รหัสสี memory addresses ไปจนถึง MAC addresses บทความนี้สรุปสิ่งสำคัญเกี่ยวกับระบบเลขฐาน 16
สารบัญ
Hexadecimal คืออะไร?
Hexadecimal หรือ hex คือ ระบบเลขฐาน 16 ระบบ decimal ที่เราใช้ทุกวันมีตัวเลข 10 ตัวคือ 0-9 แต่ hexadecimal ใช้ 16 สัญลักษณ์ คือ 0-9 และ A-F
คำนี้มาจาก Greek hexa แปลว่าหก และ Latin decem แปลว่าสิบ รวมความหมายเป็นสิบหก
ในการเขียนโปรแกรม ค่า hex มักขึ้นต้นด้วย 0x เช่น 0xFF คือ 255 ใน decimal
ทำไมเราถึงใช้ Hexadecimal?
คอมพิวเตอร์ทำงานด้วย binary แต่เลข binary จะยาวเร็วมาก ค่า 255 คือ 11111111 ใน binary แต่เขียนเป็น hex ได้สั้น ๆ ว่า FF
Hex เข้ากับ binary ได้ดีเพราะ 16 เป็นกำลังของ 2 hex หนึ่งหลักแทน binary ได้พอดี 4 bits และ hex สองหลักแทน 1 byte
- hex หนึ่งหลักแทน binary 4 หลัก ได้พอดี หรือเรียกว่า nibble
- hex สองหลักแทน 1 byte หรือ 8 bits
- แปลงระหว่าง hex และ binary ได้ง่าย แค่จัดกลุ่มทีละ 4 bits
- อ่านและเขียนได้ สั้นกว่า binary มาก
ตัวเลข Hex ทั้ง 16 ตัว
ตารางนี้แสดงตัวเลข hex ทั้งหมด พร้อมค่า decimal และ binary:
| Hex | Decimal | Binary |
|---|---|---|
0 | 0 | 0000 |
1 | 1 | 0001 |
2 | 2 | 0010 |
3 | 3 | 0011 |
4 | 4 | 0100 |
5 | 5 | 0101 |
6 | 6 | 0110 |
7 | 7 | 0111 |
8 | 8 | 1000 |
9 | 9 | 1001 |
A | 10 | 1010 |
B | 11 | 1011 |
C | 12 | 1100 |
D | 13 | 1101 |
E | 14 | 1110 |
F | 15 | 1111 |
A-F ไม่แยกตัวพิมพ์เล็กใหญ่ เช่น 0xff, 0xFF และ 0XFF คือค่าเดียวกัน
วิธีอ่านเลข Hex
Hex ทำงานคล้าย decimal แต่แต่ละตำแหน่งเป็นกำลังของ 16 แทนที่จะเป็น 10:
Decimal place values: ... 1000 100 10 1
10^3 10^2 10^1 10^0
Hex place values: ... 4096 256 16 1
16^3 16^2 16^1 16^0
Example: 0x2F4
2 x 256 = 512
F x 16 = 240 (F = 15, so 15 x 16)
4 x 1 = 4
-----
Total = 756 (decimal)การแปลง Hex เป็น Decimal
การแปลง hex เป็น decimal ทำได้โดยนำแต่ละหลักคูณกับค่าประจำตำแหน่งที่เป็นกำลังของ 16 แล้วรวมผลลัพธ์:
Example: 0x1A3
1 x 16^2 = 1 x 256 = 256
A x 16^1 = 10 x 16 = 160
3 x 16^0 = 3 x 1 = 3
-----
Result: 419
So 0x1A3 = 419 in decimal.ค่าที่ควรจำไว้:
- 0xFF = 255 ค่าสูงสุดของ 1 byte
- 0x100 = 256
- 0xFFFF = 65,535 ค่าสูงสุดของ unsigned 16-bit
- 0x7FFFFFFF = 2,147,483,647 ค่าสูงสุดของ signed integer 32-bit
การแปลง Decimal เป็น Hex
การแปลง decimal เป็น hex ทำได้โดยหารด้วย 16 ซ้ำ ๆ แล้วจดเศษ:
Example: Convert 750 to hex
750 / 16 = 46 remainder 14 (E)
46 / 16 = 2 remainder 14 (E)
2 / 16 = 0 remainder 2 (2)
Read remainders bottom-to-top: 2EE
So 750 = 0x2EEHex และ Binary
จุดเด่นของ hex คือ map กับ binary ได้สะอาดมาก hex หนึ่งหลักเท่ากับ 4 bits ดังนั้นการแปลงคือการจัดกลุ่ม bits:
Hex to Binary:
0xA7 -> A = 1010, 7 = 0111 -> 10100111
Binary to Hex:
11011110 -> 1101 = D, 1110 = E -> 0xDE
Byte example:
0xFF -> 11111111 (all bits on, max byte value)
0x00 -> 00000000 (all bits off, zero)นี่คือเหตุผลที่ hex เป็น รูปแบบยอดนิยมสำหรับดู raw bytes ใน hex dumps, memory viewers และ debuggers
Hex ในการใช้งานจริง
Hexadecimal พบได้ในหลายพื้นที่ของงานคอมพิวเตอร์และการพัฒนา:
สีใน CSS/HTML
สีถูกแทนเป็น 3 bytes คือ Red, Green และ Blue เขียนเป็น hex:
| สี | Hex Code | ค่า RGB | ตัวอย่าง |
|---|---|---|---|
| แดง | #FF0000 | 255, 0, 0 | |
| เขียว | #00FF00 | 0, 255, 0 | |
| น้ำเงิน | #0000FF | 0, 0, 255 | |
| ขาว | #FFFFFF | 255, 255, 255 | |
| ดำ | #000000 | 0, 0, 0 |
การใช้งานอื่นที่พบบ่อย
- Memory addresses - debugger มักแสดง address เช่น
0x7FFF5FBFF8A0 - MAC addresses - hardware ID ของเครือข่าย เช่น
00:1A:2B:3C:4D:5E - Unicode code points - ระบุตัวอักษร เช่น
U+1F600 - Error codes - เช่น Windows HRESULT
0x80070005 - Cryptographic hashes - SHA-256 และ MD5 มักแสดงผลเป็น hex
- IPv6 addresses - เขียนด้วยกลุ่มตัวเลข hexadecimal
Hex ในโค้ด
ภาษาโปรแกรมหลัก ๆ รองรับ hex literals และการแปลงค่า:
JavaScript
// Hex literals
const value = 0xFF; // 255
// Decimal to hex string
const hex = (255).toString(16); // "ff"
// Hex string to decimal
const dec = parseInt("ff", 16); // 255
// Pad to 2 digits (useful for colors)
const padded = (10).toString(16).padStart(2, "0"); // "0a"Python
# Hex literals
value = 0xFF # 255
# Decimal to hex string
hex_str = hex(255) # '0xff'
formatted = f"{255:02x}" # 'ff'
# Hex string to decimal
dec = int("ff", 16) # 255CSS
/* Full 6-digit hex */
color: #3B82F6;
/* Short 3-digit hex */
color: #FFF; /* same as #FFFFFF */
/* 8-digit hex with alpha transparency */
color: #3B82F680; /* 50% opacity */ข้อผิดพลาด Hex ที่พบบ่อย
ข้อผิดพลาดที่ควรระวัง:
✕ สับสนระหว่าง hex กับ decimal
10 ใน hex คือ 16 ใน decimal ไม่ใช่ 10 ควรใช้ prefix 0x หรือ # เมื่อ context อาจสับสน
✕ ลืมเติม 0 ด้านหน้า
byte ควรมี hex 2 หลักเสมอ ในบางรูปแบบควรเขียน 0A ไม่ใช่แค่ A
✕ ใช้ตัวอักษรเกิน F
Hex ใช้แค่ A-F เท่านั้น ตัวอักษร G, H หรือ Z ไม่ใช่ hex digit ที่ถูกต้อง
✕ ลืม # ใน CSS colors
สีแบบ hex ใน CSS ต้องมี prefix #
แปลงค่า Hex ได้ทันที
ใช้เครื่องมือ Hex Converter ฟรีของเราเพื่อแปลงระหว่าง hexadecimal, decimal, binary และ octal ได้ทันทีในเบราว์เซอร์
ลองใช้ Hex Converterแหล่งอ้างอิง
- Mozilla Developer Network. Hexadecimal - MDN Web Docs Glossary. https://developer.mozilla.org/en-US/docs/Glossary/Hexadecimal
- Mozilla Developer Network. <color> - CSS: Cascading Style Sheets. https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
- Mozilla Developer Network. parseInt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
- The Unicode Consortium. Unicode Code Charts. https://www.unicode.org/charts/
- IEEE. IEEE 802 - MAC Address Format. https://standards.ieee.org/products-programs/regauth/