Reproduced from: http://www.cnblogs.com/nzbbody/p/3504199.html
The int itself is represented by a string of characters, with no double quotes around it, telling the compiler to interpret it as a number. By default, it is interpreted as 10 (dec), if you want to use 8, 16, how to do. prefix and tell the compiler to interpret it according to the different system. The 8-in (OCT)---prefix plus 0,16 (hex)---prefix plus 0x or 0X. A string is preceded by a double quotation mark, telling the compiler to interpret it as a string of characters.
int into string
1. Use itoa (int value, char *string, int radix)
Converts an int integer to a string and saves the value in an array string. Where value is the integer to be converted, radix is the meaning of the cardinality, that is, first converts the value to the number of radix, and then to the string.
char *itoa (int value, char *string,int radix);
Prototype Description:
//value: data to be converted.
//String: The address of the destination string.
//Radix: The conversion of the number, can be 10, 16 into the system and so on.
//returns the pointer int aa = that points to string
;
Char c[8];
Itoa (aa,c,16);
cout<<c<<endl; 1e
Note: Itoa is not a standard C function, should try to avoid using this function, it is unique to Windows, if you want to write Cross-platform program, please use sprintf.
2. Use sprintf
<pre name= "code" class= "CPP" >//int sprintf (char *buffer, const char *format, [argument] ...);
argument list
//Buffer:char pointer to the buffer for the string to be written.
//Format: format string.
//[argument] ... : optional parameter, which can be any type of data.
//Return value: String length (strlen)
int aa =;
Char c[8];
int length = sprintf (c, "%05x", AA); x means read or output hexadecimal string
cout<<c<<endl;//0001E
3. Use StringStream
int AA =;
StringStream SS;
ss<<aa;
string S1 = Ss.str ();
cout<<s1<<endl;
string S2;
ss>>s2;
cout<<s2<<endl; 30
It can be understood that stringstream can swallow different types, according to the type of S2, and then spit out different types.
4, using the Lexical_cast in boost library
int AA =;
string s = boost::lexical_cast<string> (AA);
cout<<s<<endl; 30
3 and 4 can only be converted into 10 strings, and cannot be converted into other strings.
string into an int
1. Use Strtol (string to Long)
Standard C language standard function library quick check (Cheat Sheet) http://ganquan.info/standard-c/function/strtol
Long int strtol (const char *nptr, char **endptr, int base) Strtol () converts the string that the nptr points to, according to the argument base, to a long int, and then returns the value. The range of the parameter base is 2~36, and 0; it determines the weight of the string to be converted to an integer. When the Strtol () function detects the first illegal character, it stops detection immediately and all subsequent characters are treated as illegal characters. The legitimate string is converted to a long int as the return value of the function. An illegal string, that is, from the address of the first illegal character, is assigned to *ENDPTR. **endptr is a double pointer, a pointer to a pointer. The Strtol () function is used to change the value of *endptr, which is to pass the address of the first illegal character to Endptr. In most cases, ENDPTR is set to NULL, which means that no illegal strings are returned.
string s = "n";
Char* end;
int i = static_cast<int> (Strtol (S.c_str (), &end,16));
cout<<i<<endl;
i = static_cast<int> (Strtol (S.c_str (), &end,10));
cout<<i<<endl; 17
2. Use sscanf
<pre name= "code" class= "CPP" >int i;
SSCANF ("A", "%d", &i);
cout<<i<<endl;
sscanf ("A", "%x", &i);
cout<<i<<endl;
sscanf ("0X17", "%x", &i);
cout<<i<<endl; 23
3. Use StringStream
string s = "n";
StringStream SS;
ss<<s;
int i;
ss>>i;
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 boost library
string s = "n";
int i = boost::lexical_cast<int> (s);
cout<<i<<endl; 17