C + + int to string (StringStream can go more types)

Source: Internet
Author: User
Tags string format

first, the use of AtoiDescription

itoa (int value, char *string, int radix);
The first parameter: the int you want to convert;
The second parameter: char* after conversion;
The third parameter: the binary you want to convert;

Example:

1 //-------------------------------------2 //function: C + + int to string (using Atoi)3 //Environment: VS20054 //-------------------------------------5#include"stdafx.h"6#include <iostream>7 using namespacestd;8 intMainintargcChar*argv[])9 {Ten     intn = -; One     Charc[Ten]; AItoa (n, C,2); -cout <<"2->"<< C <<Endl; -Itoa (n, C,Ten); thecout <<"16->"<< C <<Endl; -Itoa (n, C, -); -cout <<"10->"<< C <<Endl; -System"Pause"); +     return 0; -}

Output:

2-> 11110
16-> 30
10-> 1e

second, the use of sprintf

Header file #include <stdio.h>

Syntax: int sprintf (string format, mixed [args] ...);

return value: String Length (strlen)

Convert characters

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% printed percent symbol, not converted.

b integer turns into binary.

The c integer is converted to the corresponding ASCII character.

D integers are turned into 10.

The F-Times precision number is converted to floating point numbers.

o integers are turned into octal.

The s integer is converted into a string.

The x integer is converted to lowercase 16 rounding.

X integers are converted to uppercase 16 rounding.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Example:

1 //-------------------------------------2 //function: C + + int to string (using sprintf)3 //Environment: VS20054 //-------------------------------------5#include"stdafx.h"6#include <iostream>7#include <string>8 using namespacestd;9 intMain ()Ten { One     intn = -; A     Charc[ -]; -sprintf (c,"%d", n); -cout << C <<Endl; thesprintf (c,"%o", n); -cout << C <<Endl; -sprintf (c,"%x", n); -cout << C <<Endl; +sprintf (c,"%c", n); -cout << C <<Endl; +     floatf =24.678; Asprintf (c,"%f", f); atcout << C <<Endl; -sprintf (c,"%.2f", f); -cout << C <<Endl; -sprintf (c,"%d-%.2f", N, f); -cout << C <<Endl; -System"Pause"); in     return 0; -}

Output:

30
36
1e//Note: Here is a special symbol
24.677999
24.68
30-24.68

third, the use of StringStream

Generic template (int,double,char[], recommended by Linux under compilation):

/*convert other data to Stringusage:     string str = m_tostr<int> (12345); */template <class t> string m_tostr (T tmp) {    StringStream ss;    SS << tmp;    return Ss.str ();}

  

Other examples:

-------------------------------------//function: C + + int to string (using StringStream)//environment: vs2005//------------------------ -------------#include "stdafx.h" #include <iostream> #include <string> #include <sstream>using namespace Std;int Main () {    stringstream strstream;    int a = +;    float f = 23.5566;    Strstream << a << "----" << F;    string s = Strstream.str ();    cout << s << endl;    System ("pause");    return 0;}

Output:

----23.5566
Iv. Other

1.SPRINTF can cause buffer overflow, consider using snprintf or nonstandard asprintf

2. If it is an MFC program, you can use Cstring::format

3. If you use boost, you can use it directly: string s = Boost::lexical_cast <string> (a);

4.atoi is also non-portable.

V. Other NB Methods

//-----------------------------------------------------------------------------------

Reference:

Http://baike.baidu.com/view/982195.htm?fr=ala0_1_1

Http://baike.baidu.com/view/1295144.htm?fr=ala0_1

http://pppboy.blog.163.com/blog/static/3020379620085511954382/

//-----------------------------------------------------------------------------------

Related Article

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.