// base_number_1.c// example of Binary, Octal and Hexadecimal Numbers#include<stdio.h>intmain(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 formatprintf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);//assigning value in Hexadecimal format value =0xe28;//printing value in Decimal, Octal and Hexadecimal formatprintf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);return0;}
// base_number_2.cpp// example of Binary, Octal and Hexadecimal Numbers// Credit: cppreference.com#include<iostream>#include<sstream>usingnamespace std;intmain(){ 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 numberstd::stringtoOctal(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 numberstd::stringtoBinary(int num) {return std::move((std::bitset<8>(num).to_string()));}//Function with converts the number to Hexadecimal numberstd::stringtoHex(int num) { std::string hexNumber("");while(num >0) {int rem = num %16;if(rem >9){switch(rem) {case10:hexNumber.append("A");break;case11:hexNumber.append("B");break;case12:hexNumber.append("C");break;case13:hexNumber.append("D");break;case14:hexNumber.append("E");break;case15:hexNumber.append("F");break; } }elsehexNumber.append(std::to_string(rem)); num /=16; } //Reversing the the string std::reverse(hexNumber.begin(),hexNumber.end());return(std::move(hexNumber));}std::stringconversion(int num) { std::string result("\n\nDecimal: ");result.append(std::to_string(num));result.append("\n"); //Converting to Binaryresult.append("Binary: ");result.append(toBinary(num));result.append("\n"); //Converting to Octalresult.append("Octal: ");result.append(toOctal(num));result.append("\n"); //Converting to hexresult.append("Hex: 0x");result.append(toHex(num));result.append("\n");return (std::move(result));}intmain() { 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);}
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University