Methods for converting common data types to strings in VC + + and C languages

Source: Internet
Author: User
Tags define function sprintf terminates

1. short integer (int)
Itoa (i,temp,10);/////Convert I to a string into temp, and the last digit to indicate decimal
Itoa (i,temp,2); Converting in binary mode
2. Long integer type
Ltoa (l,temp,10);

3. Floating point (float,double)
The conversion can be done with FCVT, which is an example of MSDN:
int decimal, sign;
Char *buffer;
Double Source = 3.1415926535;
Buffer = _FCVT (source,7,&decimal,&sign);
Running result: source:3.1415926535 buffer: ' 31415927 ' decimal:1 sign:0
Decimal indicates the position of the decimal point, sign indicates the symbol: 0 is positive, 1 is negative

4. CString variable
CString str= "2008 Beijing Olympics";
Char *buf = (LPSTR) (LPCTSTR) str;
CString str = "...";
int nSize = str. GetLength (+1);
Char *p = new Char[nsize];
memset (p,0, nsize*sizeof (char));
strcpy (P,STR);

5. BSTR variable
BSTR Bstrvalue =:: SysAllocString (L "programmer");
char * buf =_com_util::convertbstrtostring (bstrvalue);
SysFreeString (Bstrvalue);
AfxMessageBox (BUF);
Delete (BUF);

6. CComBSTR variable
CComBSTR Bstrvar ("test");
Char *buf = _com_util::convertbstrtostring (BSTRVAR.M_STR);
AfxMessageBox (BUF);
Delete (BUF);

7. _bstr_t variable
The _bstr_t type is the encapsulation of a BSTR, because the = operator has been overloaded, so it is easy to use
_bstr_t Bstrvar ("test");
const char *BUF = bstrvar;///do not modify the contents of BUF
AfxMessageBox (BUF);

8. Common methods (for non-COM data types)
Complete the conversion with sprintf
Char buffer[200];
char c = ' 1 ';
int i = 35;
Long j = 1000;
float F = 1.7320534f;
sprintf (buffer, "%c", c);
sprintf (buffer, "%d", I);
sprintf (buffer, "%d", j);
sprintf (buffer, "%f", f);



atof(converts a string to a floating-point number)
Correlation function Atoi,atol,strtod,strtol,strtoul

Table header file #include <stdlib.h>

Defines the function double atof (const char *nptr);

The function Description Atof () scans the parameter nptr string, skips the preceding space character until a number or sign is encountered, and then ends the conversion with a non-numeric or string ending (' + ') and returns the result. The parameter nptr string can contain a positive sign, a decimal point, or E (e) to represent an exponential portion, such as 123.456 or 123e-2.

The return value returns the number of converted floating-point types.

The additional description atof () is the same as the result of using Strtod (nptr, (char**) NULL).

atoi(converts a string to an integer number)
Correlation function Atof,atol,atrtod,strtol,strtoul

Table header file #include <stdlib.h>

Defines the function int atoi (const char *nptr);

The function Description Atoi () scans the parameter nptr string, skips the preceding space character until a number or sign is encountered, and then ends the conversion with a non-numeric or string ending (' + ') and returns the result.

The return value returns the number of integers after the conversion.

Additional instructions atoi () with the use of Strtol (nptr, (char**) null,10);

atol(convert string to growth integer)
Correlation function Atof,atoi,strtod,strtol,strtoul

Table header file #include <stdlib.h>

Defines a function long atol (const char *nptr);

The function Description Atol () scans the parameter nptr string, skips the preceding space character until a number or sign is encountered, and then ends the conversion with a non-numeric or string ending (' + ') and returns the result.

The return value returns the number of long integers after the conversion.

Additional Instructions Atol () with the use of Strtol (nptr, (char**) null,10);

gcvt(Converts a floating-point number to a string, rounding)
Correlation function ecvt,fcvt,sprintf

Table header file #include <stdlib.h>

Defines the function char *gcvt (double number,size_t Ndigits,char *buf);

The function Description GCVT () is used to convert the parameter number to an ASCII string, and the parameter ndigits indicates how many digits are displayed. GCVT () differs from ECVT () and FCVT () in that the string converted by GCVT () contains either a decimal point or a positive or negative symbol. If the conversion succeeds, the converted string is placed in the space indicated by the parameter buf pointer.

The return value returns a string pointer, which is the BUF pointer.

strtod(Convert string to floating point)
Correlation function Atoi,atol,strtod,strtol,strtoul

Table header file #include <stdlib.h>

Defines the function double strtod (const char *nptr,char **endptr);

The function Description Strtod () scans the parameter nptr string, skips the preceding space character until a number or sign is encountered, and ends the conversion until a non-digit or string ends (' + '), and returns the result. If endptr is not null, the character pointer in nptr that terminates with an unqualified condition is returned by ENDPTR. The parameter nptr string can contain a positive sign, a decimal point, or E (e) to represent the exponential portion. such as 123.456 or 123e-2.

The return value returns the number of converted floating-point types.

Strtol(Convert string to growth integer)
Correlation function Atof,atoi,atol,strtod,strtoul

Table header file #include <stdlib.h>

Defines a function long int strtol (const char *nptr,char **endptr,int base);

The function Description Strtol () converts the parameter nptr string to the number of growth integers based on the parameter base. The parameter base range is from 2 to 36, or 0. The parameter base represents the method used, such as the base value of 10 is used 10, if the base value of 16 is 16 binary. When the base value is 0, the conversion is made in 10, but encountering a pre-character such as ' 0x ' uses a 16-binary conversion. At first Strtol () scans the parameter nptr string, skips the preceding space character until it encounters a number or sign, and then ends the conversion with a non-numeric or string ending (' + ') and returns the result. If the parameter endptr is not NULL, the character pointer in nptr that terminates with an unqualified condition is returned by ENDPTR.

The return value returns the number of converted long integers, otherwise erange is returned and the error code is stored in errno.

Additional instructions Erange The specified conversion string is outside the legal range.

Strtoul (converts string to unsigned long integer)   
Related functions    atof,atoi,atol,strtod,strtol

Header file    #include <stdlib.h>

Define function    unsigned long int strtoul (const char *nptr,char **endptr,int base); The

Function Description    strtoul () converts the parameter nptr string to an unsigned long integer based on the parameter base. The parameter base range is from 2 to 36, or 0. The parameter base represents the method used, such as the base value of 10 is used 10, if the base value of 16 is 16 binary number. When the base value is 0, the conversion is made in 10, but encountering a pre-character such as ' 0x ' uses a 16-binary conversion. At first Strtoul () scans the parameter nptr string, skips the preceding space string until a number or sign is encountered, and then encounters a non-numeric or string end (' + ') ending the conversion and returning the result. If the parameter endptr is not NULL, the character pointer in nptr that terminates with an unqualified condition is returned by ENDPTR. The

Returns the value    returns the number of converted long integers, otherwise erange is returned and the error code is stored in errno.

Additional Description    Erange The specified conversion string is outside the legal range.

toascii(converts an integer number to a valid ASCII code character)
Correlation function Isascii,toupper,tolower

Table header file #include <ctype.h>

define function int toascii (int c)

The function Description Toascii () converts the parameter C to a 7-bit unsigned char value, the eighth bit is cleared, and the character is converted to ASCII characters.

The return value will convert the successful ASCII character value back

Methods for converting common data types to strings in VC + + and C languages

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.