Transferred from: http://www.cnblogs.com/nzbbody/p/3504199.html
the int itself is represented by a string of characters, with no double quotes, and tells the compiler to interpret it as a number. By default, it is interpreted as 10-binary (dec), if you want to use 8 binary, 16 binary, what should I do? Add a prefix that tells the compiler to interpret it according to the different binary. 8 binary (OCT)---prefix plus 0,16 (hex)---prefix plus 0x or 0X.
The string is preceded by a double quotation mark that tells the compiler to interpret it as a string of characters.
Note: For characters, you need to differentiate between characters and the numeric value represented by a character. For example: char a = 8;char B = ' 8 ', a for the 8th character, b for the character 8, and the 56th character.
convert int to string
1. Using itoa (int to string)
1 //char *itoa (int value, char *string,int radix), 2 //Prototype Description: 3 //value: data to be converted. 4 //string: The address of the target string. 5 //radix: The converted binary number, can be 10, 16 and so on. 6 //Returns a pointer to string that is 7 8 int aa = 9 char c[8];10 itoa (aa,c,16); cout<<c< <endl; 1e
Note: Itoa is not a standard C function, it is unique to Windows, if you want to write a cross-platform program, please use sprintf.
2. Using sprintf
1 //int sprintf (char *buffer, const char *format, [argument] ...); 2 //Parameter list 3 //Buffer:char pointer, pointing to the character to be written string of buffers. 4 //format: formatted string. 5 //[argument] ... : optional parameter, which can be any type of data. 6 //return value: String Length (strlen) 7 8 int aa = 9 char c[8]; int length = sprintf (c, "%05x", aa); 11
cout<<c<<endl; 0001E
3. Using StringStream
1 int aa = 30;2 stringstream ss;3 ss<<aa; 4 string s1 = Ss.str (); 5 cout<<s1<< Endl 306 7 String s2;8 ss>>s2;9 cout<<s2<<endl;//30
It can be understood that stringstream can swallow different types, depending on the type of S2, and then spit out the different types.
4. Using the Lexical_cast in the Boost library
1 int aa = 30;2 string s = boost::lexical_cast<string> (AA); 3 cout<<s<<endl;//30
3 and 4 can only be converted to a 10 binary string, and cannot be converted to another binary string.
Convert String to int
1. Using Strtol (string to Long)
1 string s = "n"; 2 char* end;3 int i = static_cast<int> (Strtol (S.c_str (), &end,16)); 4 cout <<i<<endl; 235 6 i = static_cast<int> (Strtol (S.c_str (), &end,10)); 7 cout<<i<<endl;//17
2. Using sscanf
1 int i;2 sscanf ("n", "%d", &i), 3 cout<<i<<endl;//174 5 sscanf ("n", "%x", &i); 6 cout<<i<<endl;//237 8 sscanf ("0X17", "%x", &i); 9 cout<<i<<endl;//23
3. Using StringStream
1 string s = "n"; 2 3 stringstream ss;4 ss<<s;5 6 int i;7 ss>>i;8 cout< <i<<endl; 17
Note: StringStream can swallow any type and spit out different types according to actual needs.
4. Using the Lexical_cast in the Boost library
1 string s = "n"; 2 int i = boost::lexical_cast<int> (s); 3 cout<<i<<endl;//17
Conversion of C + + int to string