# Binary, Octal and Hexadecimal Numbers

บ่อยครั้งที่นักพัฒนาโปรแกรมทางด้านระบบสมองกลฝังตัว จะต้องเขียนโปรแกรมเพื่อรับส่งข้อมูลตรงกับตัวฮาร์ดแวร์หรือระบบเครื่องจักร  ซึ่งจำเป็นจะต้องมีการอ่านค่าในระดับที่เป็นตัวเลขที่ไม่ใช่เลขฐานสิบที่คนทั่วไปคุ้นเคย แต่จะเป็นเลขฐานสอง เลขฐานแปด และเลขฐานสิบหก&#x20;

> **แล้วจะมีเทคนิคการเขียนด้วยภาษา C/C++ ในการจัดการค่าฐานเหล่านี้อย่างไร?**

โดยหลักการในการเขียนโปรแกรม ค่าฐานสิบ (decimal value) สามารถถูกใช้โดยไม่ต้องมีตัวอักขระกำกับ (prefix character) แต่ค่าฐานอื่น ๆ จะต้องมี prefix ดังตัวอย่างข้างล่าง

1. **ค่าเลขฐานสอง (Binary value)** จะใช้ "`0b`" (เลขศูนย์ ตามด้วยตัวพิมพ์เล็ก  ‘b’) เช่น `0b11001010`
2. **ค่าเลขฐานแปด (Octal value)** จะใช้ "`0`" (เลขศูนย์) เช่น`0533`
3. **ค่าเลขฐานสิบหก (Hexadecimal value)** จะใช้ "`0x`" หรือ  "`0X`" (เลขศูนย์ ตามด้วยตัวพิมพ์เล็กหรือพิมพ์ใหญ่  ‘x’ หรือ 'X') เช่น `0x12AB, 0x3AFF, 0Xaabf`

### ตัวอย่างการเขียนด้วยภาษา C&#x20;

```c
// base_number_1.c
// example of Binary, Octal and Hexadecimal Numbers

#include<stdio.h>

int main(void) 
{
	int value;
	
	//assigning value in Decimal format
	value = 3624;
	
	//printing value in decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in binary format
	value = 0b111000101000;
	//printing value in Decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Octal format 
	value = 07050;
	//pringing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Hexadecimal format
	value = 0xe28;
	//printing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	return 0;
}
```

ทำการ complie และดูผลลัพธ์โปรแกรม

```python
$ gcc base_number_1.c -o base_number_1
$ ./base_number_1

Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
```

### ตัวอย่างการเขียนด้วยภาษา C++

```cpp
// base_number_2.cpp
// example of Binary, Octal and Hexadecimal Numbers
// Credit: cppreference.com

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    cout << "The number 42 in octal:   " <<  oct << 42 << '\n'
         << "The number 42 in decimal: " <<  dec << 42 << '\n'
         << "The number 42 in hex:     " <<  hex << 42 << '\n';
    int n;
    istringstream("2A") >>  hex >> n;
    
    cout <<  dec << "Parsing \"2A\" as hex gives " << n << '\n';
   
     // the output base is sticky until changed
    cout <<  hex << "42 as hex gives " << 42
         << " and 21 as hex gives " << 21 << '\n';
}
```

ทำการ complie และดูผลลัพธ์โปรแกรม

```python
$ g++ base_number_2.c -o base_number_2
$ ./base_number_2

The number 42 in octal:   52
The number 42 in decimal: 42
The number 42 in hex:     2a
Parsing "2A" as hex gives 42
42 as hex gives 2a and 21 as hex gives 15
```

### ตัวอย่างประยุกต์การแปลงเลขฐานสอง ฐานแปด ฐานสิบหก

```cpp
// base_number_3.cpp
// example of Binary, Octal and Hexadecimal Numbers
// Author: Hatim Master  

#include <iostream>
#include <iterator>
#include <algorithm>
#include <bitset>//For binary conversion


//Function with converts the number to octal number
std::string toOctal(int num) {
	std::string octalNum("");
	while(num > 0) {
		int total = num % 8;
		num /= 8;
		octalNum.append(std::to_string(total));
	}
	std::reverse(octalNum.begin(),octalNum.end());
	return (std::move(octalNum));
}

//Function with converts the number to Binary number
std::string toBinary(int num) {
	return std::move((std::bitset<8>(num).to_string()));
}

//Function with converts the number to Hexadecimal number
std::string toHex(int num) {
	std::string hexNumber("");
	while(num > 0) {
		int rem = num % 16;
		if(rem > 9){
			switch(rem) {
				case 10: hexNumber.append("A");break;
				case 11: hexNumber.append("B");break;
				case 12: hexNumber.append("C");break;
				case 13: hexNumber.append("D");break;
				case 14: hexNumber.append("E");break;
				case 15: hexNumber.append("F");break;
			}
		}
		else
			hexNumber.append(std::to_string(rem));
		num /= 16;
	}
	//Reversing the the string
	std::reverse(hexNumber.begin(),hexNumber.end());
	return(std::move(hexNumber));

}


std::string conversion(int num) {
	std::string result("\n\nDecimal: ");
	result.append(std::to_string(num));
	result.append("\n");

	//Converting to Binary
	result.append("Binary: ");
	result.append(toBinary(num));
	result.append("\n");

	//Converting to Octal
	result.append("Octal: ");
	result.append(toOctal(num));
	result.append("\n");

	//Converting to hex
	result.append("Hex: 0x");
	result.append(toHex(num));
	result.append("\n");
	return (std::move(result));
}

int main() {
	std::cout << "Enter Number: ";

	std::transform(std::istream_iterator<int>(std::cin),std::istream_iterator<int>(),
			std::ostream_iterator<std::string>(std::cout,"\nNumber:"),conversion);
}
```

ทำการ complie และดูผลลัพธ์โปรแกรม

```cpp
$ g++ base_number_3.c -o base_number_3
$ ./base_number_3

Enter Number: 45

Decimal: 45
Binary: 00101101
Octal: 55
Hex: 0x2D
```
