Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Integrated Development Environment
- // Visual C++
- #include <iostream>
- #include <cstdlib>
- #define MAXBUFFER 1024
- using namespace std;
- //
- const char* HexTable = "0123456789ABCDEF";
- void DecToHex(char* BinBfr, int n, int sys);
- //
- int main(int argc, char** argv)
- {
- char buffer[MAXBUFFER];
- int nbr;
- cout << "Input decimal: ", cin >> nbr;
- DecToHex(buffer, nbr, 2);
- cout << "Binary equivalent = " << buffer << endl;
- DecToHex(buffer, nbr, 8);
- cout << "Octal equivalent = " << buffer << endl;
- DecToHex(buffer, nbr, 16);
- cout << "Hexi equivalent = 0x" << buffer << endl;
- system("PAUSE");
- return (0);
- }
- //
- void DecToHex(char* BinBfr, int n, int sys)
- {
- int a = n, b = sys, c = 0;
- unsigned int f = 0;
- while ( a != 0 )
- {
- c = a % b;
- a /= b;
- BinBfr[f ++] = HexTable[c];
- }
- BinBfr[f] = '\0';
- //
- // strrev 字串反轉函式
- //
- strrev(BinBfr);
- }
Add Comment
Please, Sign In to add comment