/อ่าน 8 นาที

ระบบเลขฐานคืออะไร? Binary, Decimal, Hex และอื่น ๆ

ตัวเลขทุกตัวถูกเขียนอยู่ในฐานใดฐานหนึ่ง เมื่อเข้าใจแนวคิดนี้แล้ว binary, octal, decimal, hexadecimal, รหัสสี, memory address และ file permissions จะอ่านง่ายขึ้นมาก

ระบบเลขฐานคืออะไร?

ระบบเลขฐาน หรือ radix คือจำนวนสัญลักษณ์ตัวเลขที่ระบบนั้นใช้ ฐานของเลขเป็นตัวกำหนดว่ามีตัวเลขอะไรให้ใช้บ้าง และแต่ละตำแหน่งมีค่าเท่าไร

ฐาน 10 หรือ decimal ใช้ตัวเลข 10 ตัว คือ 0 ถึง 9 เมื่อใช้ตัวเลขครบแล้วจึงทดไปตำแหน่งถัดไป เลข 10 จึงหมายถึงหนึ่งกลุ่มของสิบและศูนย์หน่วย

หลักการเดียวกันใช้ได้กับทุกฐาน Binary มีตัวเลข 2 ตัวคือ 0 และ 1, Octal มี 8 ตัวคือ 0 ถึง 7, ส่วน Hexadecimal มี 16 ตัวคือ 0 ถึง 9 และ A ถึง F

Positional Notation: แนวคิดสำคัญ

แนวคิดหลักของทุกระบบเลขฐานคือ positional notation หรือการที่ค่าของตัวเลขขึ้นอยู่กับตำแหน่งของมัน แต่ละตำแหน่งคือกำลังของฐานนั้น ๆ

In base B, the number d3 d2 d1 d0 equals:

  d3 x B^3 + d2 x B^2 + d1 x B^1 + d0 x B^0

Example in decimal (B=10):
  4725 = 4x10^3 + 7x10^2 + 2x10^1 + 5x10^0
       = 4000   + 700    + 20     + 5
       = 4725

Same idea in binary (B=2):
  1101 = 1x2^3 + 1x2^2 + 0x2^1 + 1x2^0
       = 8     + 4     + 0     + 1
       = 13 decimal

เมื่อเข้าใจรูปแบบนี้แล้ว การแปลงเลขฐานทุกแบบจะเป็นหลักการเดียวกัน เพียงเปลี่ยนค่าฐานเท่านั้น

เลขฐานสำคัญ 4 แบบในงานคอมพิวเตอร์

ฐานชื่อตัวเลขที่ใช้Prefixใช้กับ
2Binary0, 10bสถานะฮาร์ดแวร์ บิต logic gates และคำสั่ง CPU
8Octal0-70oUnix permissions และระบบเก่า
10Decimal0-9(ไม่มี)การนับในชีวิตประจำวันและค่าที่แสดงให้ผู้ใช้เห็น
16Hexadecimal0-9, A-F0xสี memory addresses ค่า byte และ MAC addresses

ตัวเลขค่าเดียวกันสามารถเขียนได้หลายฐานดังนี้:

DecimalBinaryOctalHexadecimal
0000
510155
10101012A
42101010522A
100110010014464
25511111111377FF
1000111110100017503E8

สังเกตว่า 255 คือ 11111111 ใน binary และ FF ใน hex นี่คือค่าสูงสุดที่เก็บได้ใน 1 byte จึงเป็นเหตุผลที่ hex นิยมมากสำหรับค่า byte

วิธีแปลงระหว่างเลขฐาน

ฐานใด ๆ เป็น Decimal

นำแต่ละหลักคูณด้วยกำลังของฐานตามตำแหน่ง แล้วบวกผลลัพธ์ทั้งหมด

Binary 10110 -> Decimal:
  1x2^4 + 0x2^3 + 1x2^2 + 1x2^1 + 0x2^0
  = 16  + 0     + 4     + 2     + 0
  = 22

Hex 2F -> Decimal:
  2x16^1 + Fx16^0
  = 32   + 15
  = 47

Octal 37 -> Decimal:
  3x8^1 + 7x8^0
  = 24  + 7
  = 31

Decimal เป็นฐานใด ๆ

หารด้วยฐานเป้าหมายซ้ำ ๆ แล้วเก็บเศษ จากนั้นอ่านเศษจากล่างขึ้นบน

Convert 47 to binary:
  47 / 2 = 23 remainder 1
  23 / 2 = 11 remainder 1
  11 / 2 =  5 remainder 1
   5 / 2 =  2 remainder 1
   2 / 2 =  1 remainder 0
   1 / 2 =  0 remainder 1

Read upward: 101111
47 decimal = 101111 binary

Convert 47 to hex:
  47 / 16 = 2 remainder 15 (F)
   2 / 16 = 0 remainder 2

Read upward: 2F
47 decimal = 2F hex

ทางลัดสำหรับฐานที่เป็นกำลังของ 2

เมื่อแปลงระหว่างฐานที่เป็นกำลังของ 2 สามารถข้าม decimal แล้วจัดกลุ่ม binary ได้โดยตรง

  • Binary เป็น octal: จัดกลุ่ม binary ทีละ 3 บิต เพราะ 2^3 = 8
  • Binary เป็น hex: จัดกลุ่ม binary ทีละ 4 บิต เพราะ 2^4 = 16
  • Octal เป็น hex: ขยาย octal เป็น binary ก่อน แล้วจัดกลุ่มใหม่เป็นชุดละ 4 บิต
Binary to Hex (group by 4 from right):
  1010 1111 0011
  = A    F    3
  = 0xAF3

Binary to Octal (group by 3 from right):
  101 011 110 011
  = 5   3   6   3
  = 0o5363

ทำไมคอมพิวเตอร์ใช้ Binary แทน Decimal?

ถ้า decimal คุ้นเคยกับมนุษย์ที่สุด ทำไมคอมพิวเตอร์จึงใช้ binary? คำตอบสั้น ๆ คือ ฟิสิกส์

  • ทรานซิสเตอร์มีธรรมชาติแบบ binary - เปิดหรือปิด นำกระแสหรือไม่นำกระแส
  • Binary ทนต่อ noise ได้ดี - แยกแรงดัน 2 ช่วงได้ง่ายและน่าเชื่อถือกว่าการแยก 10 ช่วง
  • Boolean logic เข้ากับ binary โดยตรง - การทำงาน true/false แทนได้ด้วยวงจร 1/0
  • วงจรง่าย ๆ ขยายขนาดได้ดี - วงจร binary ที่เล็กและเชื่อถือได้สามารถอัดลงชิปได้เป็นพันล้านตัว

Hex และ octal ไม่ใช่รูปแบบที่คอมพิวเตอร์ประมวลผลโดยตรง แต่เป็นวิธีเขียน binary ให้กระชับและอ่านง่ายสำหรับมนุษย์

เลขฐานอื่น ๆ ที่อาจพบได้

นอกจากเลขฐานหลักทั้ง 4 แบบ ยังมีฐานอื่นที่เจอในบางบริบท:

ฐานชื่อพบได้ที่ไหน
3Ternaryคอมพิวเตอร์ ternary เชิงทดลอง และ balanced ternary ในอัลกอริทึมบางแบบ
12Duodecimalเวลา โหล และหน่วยวัด เช่น 12 นิ้วต่อฟุต
36Base-36ID แบบสั้นและ URL shorteners ที่ใช้ 0-9 และ A-Z
58Base-58ที่อยู่แบบ Bitcoin ที่ตัดอักขระที่สับสนง่ายออก
64Base-64การ encode ข้อมูลสำหรับ email, data URI, JWT และ binary-to-text formats

การแปลงเลขฐานในโค้ด

ภาษาโปรแกรมส่วนใหญ่มีฟังก์ชันในตัวสำหรับการแปลงเลขฐานที่ใช้บ่อย:

JavaScript

// Decimal to other bases
(255).toString(2);    // "11111111" binary
(255).toString(8);    // "377" octal
(255).toString(16);   // "ff" hex
(255).toString(36);   // "73" base-36

// Other bases to decimal
parseInt("11111111", 2);  // 255
parseInt("377", 8);       // 255
parseInt("ff", 16);       // 255
parseInt("73", 36);       // 255

// Literals in code
const bin = 0b11111111;   // 255
const oct = 0o377;        // 255
const hex = 0xFF;         // 255

Python

# Decimal to other bases
bin(255)    # '0b11111111'
oct(255)    # '0o377'
hex(255)    # '0xff'

# Other bases to decimal
int("11111111", 2)   # 255
int("377", 8)        # 255
int("ff", 16)        # 255

# Any base to any base via decimal
def convert_base(number_str, from_base, to_base):
    decimal = int(number_str, from_base)
    if to_base == 10:
        return str(decimal)
    digits = []
    alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
    while decimal > 0:
        digits.append(alphabet[decimal % to_base])
        decimal //= to_base
    return ''.join(reversed(digits)) or '0'

C

#include <stdio.h>

int x = 255;

printf("Decimal: %d\n", x);  // 255
printf("Octal:   %o\n", x);  // 377
printf("Hex:     %x\n", x);  // ff
printf("Hex:     %X\n", x);  // FF

int bin = 0b11111111;  // 255 in C23 / GCC extension
int oct = 0377;        // 255
int hex = 0xFF;        // 255

การใช้งานจริง

ตัวอย่างการพบเลขฐานต่าง ๆ ในงานพัฒนาจริง:

CSS Colors (Hex)

#FF5733 หมายถึง red=255, green=87, blue=51 โดย hex 2 หลักแทนค่า color channel 1 byte

File Permissions (Octal)

chmod 755 ตั้งค่าให้ owner อ่าน/เขียน/รันได้ และให้ group/others อ่านและรันได้ แต่ละหลักแทน permission 3 บิต

Memory Addresses (Hex)

Debugger และเครื่องมือระบบแสดง address เช่น 0x7FFE1234ABCD เพราะ hex สั้นและตรงกับขอบเขต byte

IP Addresses และ Subnets (Binary)

Subnet masks เข้าใจง่ายขึ้นเมื่อดูเป็น binary เพราะ network prefix คือชุดบิต 1 ต่อด้วยบิต 0 ของ host

Short URLs (Base-36/Base-62)

URL shorteners แปลง ID ตัวเลขขนาดใหญ่เป็น string สั้น ๆ เช่น dQw4w9W

แปลงเลขระหว่างฐานใดก็ได้

ใช้เครื่องมือ Base Converter ฟรีของเราเพื่อแปลงเลขระหว่าง binary, octal, decimal, hexadecimal และฐาน 2 ถึง 36 ได้ทันทีในเบราว์เซอร์

ลองใช้ Base Converter

แหล่งอ้างอิง

  1. Knuth, D.E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Chapter 4.1: Positional Number Systems. Addison-Wesley Professional.
  2. Petzold, C. (2000). Code: The Hidden Language of Computer Hardware and Software. Microsoft Press.
  3. Mozilla Developer Network. Number.prototype.toString(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
  4. Python Software Foundation. Built-in Functions: int(). https://docs.python.org/3/library/functions.html#int
  5. IEEE Computer Society. IEEE 754-2019: Standard for Floating-Point Arithmetic. https://standards.ieee.org/ieee/754/6210/
USTHJP