In C ++, convert an integer to a numeric type.

Source: Internet
Author: User

 

In Java, integers can be easily converted into strings.
Write as follows:
Byte/short/int A = (value in the range );
String STR = "" +;
System. Out. println (STR );

This solves the problem.

However, the string in C ++ does not support such conversion.
Convert an integer int A to a string?
The solution found at night is

Long Integer long:
# Include <stdlib. h>

Char * ltoa (long value, char * string, int Radix)
Converts a long integer to an equivalent string.
The number of long integer converted by value, Radix (for example, 10 indicates decimal)
String converted string

For example
Long Lo = 34567754433l;
Char * CHA = new char [64];
Ltoa (Lo, cha, 10 );

After running, the cha structure becomes the string whose content looks like Lo.

Integer INT:
# Include <stdlib. h>
Char * ITOA (INT value, char * string, int Radix)
Converts an integer value to an equivalent string.
String converted string

Same usage as long

Then use string

Here is just an imaginary step. In actual application, the number of digits of the int can be calculated, and the length is equal to the number of digits of the integer.Add one or add twoCharacter array

PS: Because the string ends with '\ 0', the last character must be kept and appended with a length of + 1.

PS2: if it is a negative number, leave a position at the beginning with the plus or minus sign '-' Length + 1

In addition, if vc2008 uses the ITOA command, the compiler will warn

Warning c4996: 'itoa ': the POSIX name for this item is deprecated. Instead, use the Iso c ++ conformant name: _ ITOA. See online help for details.

I am not in favor of using ITOA, because it may cause name conflicts and so on due to transplantation.

Finally, put the simulated ITOA code found on the Internet.

/// * ================================================= ========================================================== ===

/// Function: converts an integer to a string.
/// Parameter description: int nvalue [in]: The integer to be converted
/// Char * szstring [out]: returns the converted string.
/// Int Radix [in]: Base (base)
//// Return value: pointer to the Conversion Result
/// Supplement: after testing, we found that this function has a defect: no negative number can be recognized ..........
////------------------------------------------------------------------------------

// Iflytek_embeded: sszhou: Create: 2004-01-30
========================================================== ======================================================= */

Char * ifly_itoa (INT nvalue, char * szstring, int Radix)
{
Int I = 0, Len = 0;
Int ntemp = nvalue;
Char * lpstring = szstring;

/* Get the symbol */
If (nvalue <0)
{
Lpstring [0] = '-';
Lpstring ++;
Ntemp * =-1;
}

/* Input szstring in reverse order */
For (I = 0; ntemp! = 0; I ++)
{
Lpstring [I] = ntemp % Radix;
/* Convert numeric values to symbols */
If (lpstring [I] <= 9)
{
Lpstring [I] + = 0x30;
}
Else
{
Lpstring [I] + = 0x37;
}
Ntemp/= Radix;
}

/* Solve the 0 problem */
If (I = 0)
{
Lpstring [0] = 0x30;
I ++;
}

/* Obtain the correct sequence */
Len = I;
For (I = 0; I <(LEN/2); I ++)
{
Ntemp = lpstring [I];
Lpstring [I] = lpstring [Len-i-1];
Lpstring [Len-i-1] = ntemp;
}

Lpstring [Len] = 0;
 

Return szstring;
};

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.