Conversion of various characters, string types

Source: Internet
Author: User
Tags sprintf

Itoa function: Convert an integer to a string
Usage: char *itoa (int value, char *string, int radix);
Explanation: ITOA is an abbreviation for the English Integer to array (converting int integer to a string and storing the value in the array string).
Parameters:
Value: the integer to convert.
Radix: Is the meaning of the cardinality, that is, the value is converted to the number of radix, the range is between 2-36, for example, 10 means 10, 16 means 16 binary.
* String: The string to be saved after the conversion.
return value:
char *: Points to the generated string, with *string.

Note: The function's header file is "Stdlib.h"

#include <stdlib.h>

#include <stdio.h>

int main ()

{

int number = 123456;

Char string[25];

Itoa (number, string, 10);

printf ("integer =%d string =%s\n", number, string);

return 0;

}

Atoi C language Library function name: atoi
Function: Converts a string to an integer number
Function Description: Atoi () scans the parameter nptr string, detects the first number or positive sign when the type conversion is started, and then detects a non-numeric or terminator when the conversion is stopped, returning an integer number.
Prototype: int atoi (const char *nptr);
Header files to use: #include <stdlib.h>
program Example:
1)
#include <stdlib.h>
#include <stdio.h>
int main (void)
{
int n;
Char *str = "12345.67";
n = atoi (str);
printf ("string =%s integer =%d\n", str, n);
return 0;
}
Execution results

string = 12345.67 integer = 12345

String Turn CString
Cstring.format ("%s", String.c_str ());

Char Turn CString
Cstring.format ("%s", char*);

Char to String
string S (char *);

String to char *
Char *p = STRING.C_STR ();

CString Turn string
String s (Cstring.getbuffer ());

1,string-CString
Cstring.format ("%s", String.c_str ());
Using C_str () is really better than data ().
2,char, String
string S (char *);
You can initialize only, preferably in a place that is not initialized or with assign ().
3,cstring, String
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise the space occupied by the buffer will not be released.


In the C + + standard function library.
There are three functions to convert the contents of a string to a character array and c-string
1.data (), returns an array of strings without "\"
2,C_STR (), returns an array of strings with "\"
3,copy ()

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

Conversion between CString and int, char*, char[100]--


Conversion between CString and int, char*, char[100]--



CString Mutual Transfer int

To convert a character to an integer, you can use Atoi, _atoi64, or ATOL.
Instead of converting a number to a CString variable, you can use the CString format function. Such as
CString s;
int i = 64;
S.format ("%d", i)
The Format function is very powerful and deserves 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);

CString Mutual Transfer char*

char * to CString
CString strtest;
char * CHARPOINT;
Charpoint= "Give string a value";
Strtest=charpoint;


CString to char *
Charpoint=strtest. GetBuffer (strtest. GetLength ());

No String,char *==char in standard C []==string

You can use the Cstring.format ("%s", char *) method to turn char * into CString. To turn CString into char *, use the operator (LPCSTR) to CString.


CString conversion char[100]

Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));

Convert CString type to int
To convert a character to an integer, you can use Atoi, _atoi64, or ATOL.

CString AAA = "16";
int int_chage = Atoi ((LPCSTR) AAA);


Instead of converting a number to a CString variable, you can use the CString format function. Such as
CString s;
int i = 64;
S.format ("%d", i)
The Format function is very powerful and deserves your research.
If you are using a char array, you can also use the sprintf function.

CString ss= "1212.12";
int Temp=atoi (ss);
CString AA;
Aa. Format ("%d", temp);


Numbers--strings in addition to using Cstring::format, there are FORMATV, sprintf and itoa that do not need AFX

#include <stdlib.h>

int atoi (const char *nptr);
Long Atol (const char *nptr);
Long Long Atoll (const char *nptr);
Long Long Atoq (const char *nptr);

String AA ("AAA");
Char *c=aa.c_str ();


Cannot convert from ' const char * ' to ' char * '
const char *C=AA.C_STR ();

STRING.C_STR () can only be converted to a const char *,
To turn to char * write this:

String Mngname;
Char t[200]; memset (t,0,200); strcpy (T,mngname.c_str ());

Conversion of various characters, string types

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.