Define your own itoa function to implement various hexadecimal conversions. Run the code below.
Code
1 # include <iostream>
2 using namespace std;
3/* Custom itoa functions */
4 char * myitoa (int x, char * str, int l );
5 void main ()
6 {
7
8 int s = 0;
9 char d [10];
10 cout <"input :";
11 cin> s;
12 myitoa (s, d, 10); // decimal
13 cout <"decimal:" <d <endl;
14 myitoa (s, d, 8); // decimal
15 cout <":" <d <endl;
16 myitoa (s, d, 16); // decimal
17 cout <"hexadecimal:" <d <endl;
18}
19
20 char * myitoa (int x, char * ptr, int l)
21 {
22 const char table [] = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // a bit like dictionary Query
23 bool flag = true;
24 char * str = ptr;
25 if (x = 0)
26 {
27 * str ++ = '0 ';
28 * str = '\ 0 ';
29 return str;
30}
31 if (x <0)
32 {
33 * str ++ = '-';
34 x =-x;
35 flag = false;
36}
37 while (x)
38 {
39 * str ++ = table [x % l]; // corresponding hexadecimal
40 x = x/l;
41}
42 * str = '\ 0 ';
43 char * start = flag? Ptr: ptr + 1;
44 str --;
45/* reverse output */
46 while (start <str)
47 {
48 char tmp = * start;
49 * start = * str;
50 * str = tmp;
51 start ++;
52 str --;
53}
54
55 return ptr;
56
57}
58
Initial: http://www.5dkx.com/arch/64.html
Non-special instructions are original articles such as reprint, please note: reprint from 5D happy blog [http://www.5DKX.com/]