Method of conversion between numbers and strings in C + + (recommended) _c language

Source: Internet
Author: User
Tags sprintf uppercase letter

1, the conversion between string numbers

(1) string--> char *
  string str ("OK");
  char * p = str.c_str ();

(2) char *-->string
  char *p = "OK";
  String str (p);

(3) char *-->cstring 
  char *p = "OK";
  CString m_str (p);
  or
  CString m_str;
  M_str.format ("%s", p);

(4) CString--> char *
  CString str ("OK");
  char * p = str. GetBuffer (0);
  ...
  Str. ReleaseBuffer ();

(5) String--> CString 
  cstring.format ("%s", String.c_str ()); 

(6) CString--> String
  string s (Cstring.getbuffer (0)); 
  GetBuffer () must be releasebuffer (), otherwise there is no free buffer space occupied, CString objects can not dynamically grow.

(7) double/float->cstring
  double data;
  Cstring.format ("%.2f", data); Retains 2 decimal places

(8) cstring->double
  CString s= "123.12";
  Double  D=atof (s);  
 
(9) String->double
 double D=atof (S.c_str ());

2. Digital spin string: Using the sprintf () function

Char str[10];
int a=1234321;
sprintf (str, "%d", a);
--------------------
Char str[10];
Double a=123.321;
sprintf (str, "%.3LF", a);
--------------------
Char str[10];
int a=175;
sprintf (str, "%x", a);//10 is converted to 16, if the output uppercase letter is sprintf (str,%x, a)
--------------------
char *itoa (int Value, char* string, int radix); 

You can also spin a number to a string, but itoa () is a platform-dependent (not a standard), so this function is not recommended here.

3. String number: Using the sscanf () function

Char str[]= "1234321";
int A;
SSCANF (str, "%d", &a);
.............
Char str[]= "123.321";
Double A;
SSCANF (str, "%LF", &a);
.............
Char str[]= "AF";
int A;
SSCANF (str, "%x", &a); 16-in-system conversion to 10-system

Alternatively, you can use Atoi (), Atol (), Atof ().

4, the use of StringStream class

Write a string with the Ostringstream object, similar to sprintf ()

 Ostringstream S1;
 int i =;
 S1 << "Hello" << i << Endl;
 String s2 = S1.str ();
 cout << S2;

Read a string with the Istringstream object, similar to SSCANF ()

 Istringstream stream1;
 string string1 = "n";
 STREAM1.STR (string1);
 int i;
 Stream1 >> i;
 cout << i << Endl; Displays 25

The above is a small series of C + + in the number and string between the conversion method (recommended) of the whole content, I hope to help you, a lot of support cloud Habitat Community ~

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.