convert integers to Strings

Source: Internet
Author: User

1. Using the Itoa function

Char *itoa( int Value , char *string,int radix); prototype Description: value: The data that you want to convert. string: The address of the target string. Radix: Converted binary number, can be 2 binary, 8, 10, 16, etc. Exmaple:
#include <stdlib.h>#include<stdio.h>intMainvoid){    intNumber =12345; Char string[ +]; Itoa (number,string,Ten); printf ("integer =%d string =%s\n", number,string); return 0;}

2. Do not use library functions to convert to C-style strings

voidInttostring (intNumChar*s) {    inti =0;  while(num) {s[i]= num%Ten+'0'; Num= num/Ten; I++; } S[i]=' /';  for(intj =0, k = i1; J < K; j++,k--)    {        Chartemp =S[j]; S[J]=S[k]; S[K]=temp; }}

3. Using string streams to implement

string inttostring (int  n) {    ostringstream ostr;    Ostr<<N;     return ostr.str ();}

4. Using string implementations

string intToString2 (int  n) {    string"";      while (n)    {        char c = n%'0';         = c+result;         = n/;    }     return result;}

You can also use string to get the char* pointer

string str = inttostring (n); Char * s = const_cast<char*> (STR.C_STR ());

convert integers to Strings

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.