// 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;
}
// 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 และดูผลลัพธ์โปรแกรม
$ g++ base_number_2.c -o base_number_2$ ./base_number_2The number 42in octal:52The number 42in decimal:42The number 42inhex: 2aParsing "2A"ashex gives 4242ashex gives 2a and21ashex gives 15
ตัวอย่างประยุกต์การแปลงเลขฐานสอง ฐานแปด ฐานสิบหก
// 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);
}