Custom itoa functions for various hexadecimal Transformations

Source: Internet
Author: User

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/]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.