C ++ type conversion atoi atol atof, ITOA ftoa char string

Source: Internet
Author: User
Http://blog.163.com/chen_dawn/blog/static/1125063201011203536852/
1. atoi
int atoi ( const char * str );

Convert string to integer

Parses the C stringStrInterpreting its content as an integral number, which is returned as
IntValue.

/* atoi example */#include <stdio.h>#include <stdlib.h>int main (){  int i;  char szInput [256];  printf ("Enter a number: ");  fgets ( szInput, 256, stdin );  i = atoi (szInput);  printf ("The value entered is %d. The double is %d.\n",i,i*2);  return 0;}

Output

Enter a number: 73The value entered is 73. The double is 146.
2. atol
long int atol ( const char * str );

Convert string to long integer

Parses the C stringStrInterpreting its content as an integral number, which is returned as
Long intValue.

/* atol example */#include <stdio.h>#include <stdlib.h>int main (){  long int li;  char szInput [256];  printf ("Enter a long number: ");  gets ( szInput );  li = atol (szInput);  printf ("The value entered is %d. The double is %d.\n",li,li*2);  return 0;}

Output

Enter a number: 567283The value entered is 567283. The double is 1134566.
3. atof
 
double atof ( const char * str );

Convert string to double

Parses the C stringStrInterpreting its content as a floating point number and returns its value as
Double.

/* atof example: sine calculator */#include <stdio.h>#include <stdlib.h>#include <math.h>int main (){  double n,m;  double pi=3.1415926535;  char szInput [256];  printf ( "Enter degrees: " );  gets ( szInput );  n = atof ( szInput );  m = sin (n*pi/180);  printf ( "The sine of %f degrees is %f\n" , n, m );  return 0;}OutputEnter degrees: 45The sine of 45.000000 degrees is 0.707101
4. ITOA
char *  itoa ( int value, char * str, int base );

Convert integer to string (non-standard function)

Converts an integerValueTo a null-terminated string using the specified
BaseAnd stores the result in the array givenStrParameter.

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.A standard-compliant alternative for some cases may be sprintf:
  • Sprintf (STR, "% d", value) converts to decimal base.
  • Sprintf (STR, "% x", value) converts to hexadecimal base.
  • Sprintf (STR, "% O", value) converts to octal Base

/* itoa example */#include <stdio.h>#include <stdlib.h>int main (){  int i;  char buffer [33];  printf ("Enter a number: ");  scanf ("%d",&i);  itoa (i,buffer,10);  printf ("decimal: %s\n",buffer);  itoa (i,buffer,16);  printf ("hexadecimal: %s\n",buffer);  itoa (i,buffer,2);  printf ("binary: %s\n",buffer); return 0;}

Output

Enter a number: 1750decimal: 1750hexadecimal: 6d6binary: 11011010110
5. ftoa

#include <sstream>string convertDouble(double value) {  std::ostringstream o;  if (!(o << value))    return "";  return o.str();}
6. string to Char
1. the const char * c_str (); c_str () function returns a pointer to a regular C string with the same content as the string. this is to be compatible with the C language and has no string type in the C language. Therefore, you must use the member function c_str () of the string class object to convert the string object to the string style in C. Note: You must use strcpy () and other functions to operate the pointer returned by c_str (). For example, it is best not to use the following code: char * C; string S = "1234"; C = S. c_str (); // The final content pointed to by C is garbage. Because the S object is destructed, the content should be processed as follows: Char C [20]; string S = "1234"; strcpy (C, S. c_str (); in this way, no error occurs. c_str () returns a temporary pointer and cannot operate on it (only copy it () returns the string containing a string in the form of char *. If a function requires the char * parameter, you can use the c_str () method: String S = "Hello world! "; Printf (" % s ", S. c_str (); // output" Hello world! "2. Const * char c_str () a function that converts string to const * char. The pointer returned by string c_str () is managed by string. Its life cycle is the life cycle of the string object. Then you can use this pointer in the form of C or copy its content. Example: String s; CIN> S; const char * Ch = S. c_str (); in this way, you can input any long string from the standard input and use it according to const * char. 3. to convert a char to a string, you can use string S (char *); 4. other conversion methods: Convert string to cstring. format ("% s", String. c_str (); Char to cstring. format ("% s", char *); notes:
[Conversion] C ++ int, Char, String, cstring type conversion (Summary)

# Include <string>// When using the string class of the C ++ standard library

Using namespace STD;// Same as above

# Include <sstream>

# Include <iostream>

# Include <stdlib. h>// It is best to directly convert the string and INT types to include,

// Because it is easier to write a conversion function by yourself, the function definition is as follows:

String getstring (const int N)

{

STD: stringstream newstr;
Newstr <N;
Return newstr. STR ();

}

String to cstring
Cstring. Format ("% s", String. c_str ());

Convert Char to cstring
Cstring. Format ("% s", char *);

Char to string
String S (char *);

String to char *
Char * P = string. c_str ();

Cstring to string
String S (cstring. getbuffer ());

1, string-> cstring
Cstring. Format ("% s", String. c_str ());
C_str () is indeed better than data.
2, char-> string
String S (char *);
Only Initialization is allowed. It is best to use assign () instead of initialization ().
3, cstring-> string
String S (cstring. getbuffer ());
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.

As mentioned in C ++ standard function library
There are three functions that can convert the content of a string to a character array and a C-string
1. Data (), returns a string array without "\ 0"
2, c_str (), returns a string array with "\ 0"
3, copy ()

---------------------

Conversion between cstring, Int, char *, and char [100 --

Conversion between cstring, Int, char *, and char [100 --

Cstring mutual int Conversion

Converts a character to an integer. You can use atoi, _ atoi64, or atol.
To convert a number to a cstring variable, you can use the format function of cstring. For example
Cstring S;
Int I = 64;
S. Format ("% d", I)
The format function is very powerful and worth your research.

Void cstrdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Cstring
Ss = "1212.12 ″;
Int temp = atoi (SS );
Cstring AA;
AA. Format ("% d", temp );
Afxmessagebox ("VaR is" + AA );
}

Sart. Format ("% s", Buf );

Convert cstring to char *

/// Char * To cstring
Cstring strtest;
Char * charpoint;
Charpoint = "give string a value ";
Strtest = charpoint;

/// Cstring to char *
Charpoint = strtest. getbuffer (strtest. getlength ());

There is no string in Standard C, char * = char [] = string

You can use the cstring. Format ("% s", char *) method to convert char * To cstring. To convert cstring to char *, use the operator (lpcstr) cstring.

Cstring conversion char [100]

Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());

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.