1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <climits>
void get_hex_digits(int, int*);
int main() {
int hexnum;
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << "Enter hex number: ";
std::cin.setf(std::ios::hex, std::ios::basefield);
std::cin >> hexnum;
int separatenumbers[(sizeof(hexnum) * 16) / CHAR_BIT];
get_hex_digits(hexnum, separatenumbers);
std::cout.setf(std::ios::dec, std::ios::basefield);
std::cout << "\nIn decimal, it is " << hexnum << "\n";
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << "0x" << hexnum << " in seperate digits is:\n\t";
for (int i = 0; i < sizeof(separatenumbers); i++) {
std::cout.setf(std::ios::dec, std::ios::basefield);
std::cout << i + 1 << ". 0x";
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << separatenumbers[i] << "\n\t";
}
std::cout << std::endl;
return 0;
}
void get_hex_digits(int hexnumber, int* array) {
int i = 0, j = (sizeof(hexnumber) * 16) / CHAR_BIT, k = j;
for (; i < j; i++, k--) {
array[i] = (hexnumber >> k) & 0xF;
}
}
|
Enter hex number: 0xABCDEFG
In decimal, it is 11259375
0xabcdef in seperate digits is:
1. 0xd
2. 0xb
3. 0x7
4. 0xf
5. 0xe
6. 0xd
7. 0xb
8. 0x7
9. 0x0
10. 0x0
11. 0xa
12. 0xabcdef
13. 0xef3fd440
14. 0x7fff
15. 0x400c50
16. 0x0
17. 0x0
18. 0x0
19. 0x323ce5a6
20. 0x7fda
21. 0x0
22. 0x0
23. 0xef3fd448
24. 0x7fff
25. 0x0
26. 0x1
27. 0x4009ee
28. 0x0
29. 0x400c50
30. 0x0
31. 0xbc108f94
32. 0x2ae81c36 |