Number and Conversion Techniques เทคนิคการแปลงเลขฐาน
Convert between denary, binary, and hexadecimal แปลงระหว่างฐานสิบ ฐานสอง และฐานสิบหก
You can convert between denary, binary, and hexadecimal by hand using two ideas:
place values (multiply each digit by its column value, then add) and
repeated division by the target base (then read the remainders).
Python int(…, base), bin(), and hex() give the same answers —
use them only to check your work.
เราแปลงระหว่างฐานสิบ ฐานสอง และฐานสิบหกด้วยมือได้ด้วยแนวคิดหลัก 2 อย่าง คือ
ค่าประจำหลัก (คูณเลขแต่ละหลักด้วยค่าประจำหลัก แล้วบวกกัน) และ
การหารซ้ำด้วยฐานปลายทาง (แล้วอ่านเศษ)
ส่วน Python int(…, base), bin() และ hex() ให้คำตอบเดียวกัน
ใช้เพียงเพื่อตรวจคำตอบ
Handy conversion ideas แนวคิดที่ใช้บ่อย
- Any base → base 10: multiply each digit by its place value (\(b^{N}\)) and add. ฐานใด ๆ → ฐาน 10: คูณเลขแต่ละหลักด้วยค่าประจำหลัก (\(b^{N}\)) แล้วบวกกัน
- Base 10 → base 2: divide by 2 repeatedly, read remainders bottom‑to‑top. ฐาน 10 → ฐาน 2: หารด้วย 2 ซ้ำ ๆ อ่านเศษจากล่างขึ้นบน
- Base 10 → base 16: divide by 16 repeatedly, read remainders bottom‑to‑top (10–15 → A–F). ฐาน 10 → ฐาน 16: หารด้วย 16 ซ้ำ ๆ อ่านเศษจากล่างขึ้นบน (เศษ 10–15 เขียนเป็น A–F)
- Base 16 → base 2: replace each hex digit with its 4‑bit group. ฐาน 16 → ฐาน 2: แทนแต่ละหลักฐานสิบหกด้วยเลขฐานสอง 4 บิต
- Base 2 → base 16: group bits into 4s from the right, pad with leading zeros, convert each group to one hex digit. ฐาน 2 → ฐาน 16: แบ่งบิตเป็นกลุ่มละ 4 จากขวา เติมศูนย์นำหน้าให้ครบ แล้วแปลงแต่ละกลุ่มเป็นเลขฐานสิบหก 1 หลัก
Converting any base to base 10 การแปลงเลขฐานใด ๆ เป็นฐาน 10
- Step 1: multiply each digit by its place value (\(d \times b^{N}\)). Step 1: นำค่าของเลขโดดคูณด้วยค่าประจำหลัก (\(d \times b^{N}\))
- Step 2: add the results of every column. Step 2: หาผลรวมของทุกหลัก
Example 1: binary 1111 1111
ตัวอย่าง 1 เลขฐาน 2 คือ 1111 1111
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | \(1 \times 2^{7}\) | \(1 \times 2^{6}\) | \(1 \times 2^{5}\) | \(1 \times 2^{4}\) | \(1 \times 2^{3}\) | \(1 \times 2^{2}\) | \(1 \times 2^{1}\) | \(1 \times 2^{0}\) | 255 |
| Step 2 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | 255 |
Example 2: hexadecimal FF
ตัวอย่าง 2 เลขฐาน 16 คือ FF
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | - | - | - | - | - | - | \(15 \times 16^{1}\) | \(15 \times 16^{0}\) | 255 |
| Step 2 | - | - | - | - | - | - | 240 | 15 | 255 |
Converting base 10 to base 2 การแปลงเลขฐาน 10 เป็นฐาน 2
- Method: take the base‑10 number, divide by 2 repeatedly, and record the quotient and remainder each time. Stop when the quotient is 0, then read the remainders from bottom to top. วิธีการ คือเอาตัวเลขฐาน 10 ตั้ง แล้วหารด้วย 2 ซ้ำ ๆ บันทึกผลหารและเศษไว้ทุกครั้ง ทำจนผลหารเป็น 0 แล้วอ่านเศษจากล่างขึ้นบน
Example: convert 101 (base 10)
ตัวอย่าง กำหนดให้เลข 101 เป็นเลขฐาน 10
| Denary เลขฐานสิบ | ÷ 2 quotient ผลหาร | Remainder เศษ |
|---|---|---|
| 101 | 50 | 1 |
| 50 | 25 | 0 |
| 25 | 12 | 1 |
| 12 | 6 | 0 |
| 6 | 3 | 0 |
| 3 | 1 | 1 |
| 1 | 0 | 1 |
Reading the remainders bottom‑to‑top gives 1100101; padding to 8 bits: 0110 0101.
อ่านเศษจากล่างขึ้นบนได้ 1100101 เติมศูนย์นำหน้าให้ครบ 8 บิต ได้ 0110 0101
Check by converting 0110 0101 back to base 10:
ทดสอบแปลงเลขฐาน 2 0110 0101 กลับเป็นเลขฐาน 10:
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | \(0 \times 2^{7}\) | \(1 \times 2^{6}\) | \(1 \times 2^{5}\) | \(0 \times 2^{4}\) | \(0 \times 2^{3}\) | \(1 \times 2^{2}\) | \(0 \times 2^{1}\) | \(1 \times 2^{0}\) | 101 |
| Step 2 | 0 | 64 | 32 | 0 | 0 | 4 | 0 | 1 | 101 |
Converting base 10 to base 16 การแปลงเลขฐาน 10 เป็นฐาน 16
- Method: same as base 10 → base 2, but divide by 16 each time and read the remainders bottom‑to‑top. A remainder of 10–15 is written as A–F. วิธีการ เหมือนการแปลงฐาน 10 → ฐาน 2 แต่หารด้วย 16 ทุกครั้ง แล้วอ่านเศษจากล่างขึ้นบน เศษที่มีค่า 10–15 เขียนเป็น A–F
Example: convert 101 (base 10)
ตัวอย่าง กำหนดให้เลข 101 เป็นเลขฐาน 10
| Denary เลขฐานสิบ | ÷ 16 quotient ผลหาร | Remainder เศษ |
|---|---|---|
| 101 | 6 | 5 |
| 6 | 0 | 6 |
Reading the remainders bottom‑to‑top gives 6, 5 → 65 in base 16.
อ่านเศษจากล่างขึ้นบนได้ 6, 5 → เลขฐาน 16 คือ 65
Check by converting 65 (base 16) back to base 10:
ทดสอบแปลงเลขฐาน 16 65 กลับเป็นเลขฐาน 10:
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | - | - | - | - | - | - | \(6 \times 16^{1}\) | \(5 \times 16^{0}\) | 101 |
| Step 2 | - | - | - | - | - | - | 96 | 5 | 101 |
Converting base 16 to base 2 การแปลงเลขฐาน 16 เป็นฐาน 2
- Method: split the number into single digits and replace each hex digit with exactly 4 bits (from the table below). Join the groups in order. วิธีการ คือแยกตัวเลขออกเป็นทีละ 1 หลัก แล้วแทนแต่ละหลักฐานสิบหกด้วยเลขฐานสอง 4 บิตเสมอ (ตามตารางด้านล่าง) จากนั้นนำแต่ละกลุ่มมาเรียงต่อกัน
| Base 16 ฐาน 16 | Base 2 ฐาน 2 | Base 16 ฐาน 16 | Base 2 ฐาน 2 |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
Example: hexadecimal 65 → 6 is 0110, 5 is 0101 → 0110 0101.
ตัวอย่าง เลขฐาน 16 คือ 65 → 6 คือ 0110, 5 คือ 0101 → 0110 0101
Check by converting 0110 0101 back to base 10:
ทดสอบแปลงเลขฐาน 2 0110 0101 กลับเป็นเลขฐาน 10:
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | \(0 \times 2^{7}\) | \(1 \times 2^{6}\) | \(1 \times 2^{5}\) | \(0 \times 2^{4}\) | \(0 \times 2^{3}\) | \(1 \times 2^{2}\) | \(0 \times 2^{1}\) | \(1 \times 2^{0}\) | 101 |
| Step 2 | 0 | 64 | 32 | 0 | 0 | 4 | 0 | 1 | 101 |
Converting base 2 to base 16 การแปลงเลขฐาน 2 เป็นฐาน 16
- Method: group the bits into 4s starting from the right. If the left‑most group has fewer than 4 bits, pad it with leading zeros. Convert each 4‑bit group to one hex digit (use the 4‑bit table above). วิธีการ คือแบ่งเลขฐาน 2 ออกเป็นกลุ่มละ 4 บิต โดยเริ่มนับจากขวาสุด ถ้ากลุ่มซ้ายสุดไม่ครบ 4 บิต ให้เติมศูนย์นำหน้าจนครบ แล้วแปลงแต่ละกลุ่ม 4 บิตเป็นเลขฐานสิบหก 1 หลัก (ใช้ตาราง 4 บิตด้านบน)
Example 1: 0110 0101 → 0110 is 6, 0101 is 5 → 65 in base 16.
ตัวอย่าง 1 0110 0101 → 0110 คือ 6, 0101 คือ 5 → เลขฐาน 16 คือ 65
Example 2 (with padding): 110101 → split from the right 11 | 0101 → pad the left group 0011 | 0101 → 3, 5 → 35 in base 16.
ตัวอย่าง 2 (มีการเติมศูนย์) 110101 → แบ่งจากขวา 11 | 0101 → เติมศูนย์กลุ่มซ้าย 0011 | 0101 → 3, 5 → เลขฐาน 16 คือ 35
Check by converting 65 (base 16) back to base 10 — it matches 0110 0101 from Example 1:
ทดสอบแปลงเลขฐาน 16 65 กลับเป็นเลขฐาน 10 — ตรงกับ 0110 0101 ในตัวอย่าง 1:
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total (base 10) รวม (เลขฐาน 10) |
|---|---|---|---|---|---|---|---|---|---|
| Step 1 | - | - | - | - | - | - | \(6 \times 16^{1}\) | \(5 \times 16^{0}\) | 101 |
| Step 2 | - | - | - | - | - | - | 96 | 5 | 101 |
Check your work with Python ตรวจคำตอบด้วย Python
The by‑hand methods above are the point of this lesson. These optional demos just confirm the same
answers with int(…, base), bin(), and hex().
วิธีทำด้วยมือด้านบนคือหัวใจของบทนี้ ตัวอย่างด้านล่างเป็นทางเลือกไว้ยืนยันคำตอบเดียวกันด้วย
int(…, base), bin() และ hex()
1 Binary → denary with int(…, 2)
1 ฐานสอง → ฐานสิบ ด้วย int(…, 2)
int("111001", 2) parses a binary digit string as base-2 and returns denary.
For a smaller check, expand 1101 by place values: 8+4+0+1.
int("111001", 2) อ่านสตริงฐานสองเป็น base-2 แล้วคืนค่าฐานสิบ
ตรวจด้วยเลขเล็ก 1101 โดยกระจายค่าประจำหลัก: 8+4+0+1
57 13 13
2 Denary → binary with bin()
2 ฐานสิบ → ฐานสอง ด้วย bin()
By hand you repeatedly divide by 2 and read remainders from bottom to top.
bin(135) does the conversion and returns a string prefixed with 0b.
แบบมือคือหารด้วย 2 ซ้ำแล้วอ่านเศษจากล่างขึ้นบน
bin(135) แปลงให้และคืนสตริงที่ขึ้นต้นด้วย 0b
0b10000111 135
3 Hex ↔ binary / denary 3 ฐานสิบหก ↔ ฐานสอง / ฐานสิบ
int("3C", 16) and hex(60) move between hex and denary.
Conceptually, 3C is two 4-bit groups: 0011 and 1100 → 00111100.
int("3C", 16) และ hex(60) แปลงระหว่างฐานสิบหกกับฐานสิบ
ตามแนวคิด 3C คือสองกลุ่ม 4 บิต: 0011 และ 1100 → 00111100
60 0x3c 60 True
Note: Run uses a JavaScript simulator, not real Python. For real Python, try Google Colab. หมายเหตุ: ปุ่ม Run จำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการ Python จริง ลองใช้ Google Colab
References References / แหล่งอ้างอิง
Sources used for conversion techniques:
แหล่งที่ใช้ประกอบเนื้อหาเทคนิคการแปลงเลข:
- Number base conversion — Wikipedia — Binary number (conversion) / Hexadecimal (conversion) (place-value sums, repeated division, and 4-bit digit groups) (ผลรวมค่าประจำหลัก การหารซ้ำ และกลุ่ม 4 บิต)
-
Python
int/bin/hex— Built-in Functions — int / bin / hex (used in the check-your-work demos) (ใช้ในตัวอย่าง Run สำหรับตรวจคำตอบ)