C + + implements Atoi () and Itoa () functions (String and Integer conversions)

Source: Internet
Author: User
Tags sprintf

One: Cause

(1) The conversion of a string type to an integer type (integer) or a string type (string) into a double type, which has very good intrinsic functions in Java, very easy things;

(2) But in C there is no integer double, such as packaging class, from char[] array into an integer type becomes less simple, atoi () itoa () in widows below, but the online said Linux does not seem to have itoa () function, with sprintf () OK, but I tested the sprintf () sscanf () is inefficient.

(3) So I manually implemented a bit atoi () (String to Integer) itoa (integer to String) two functions, where the wrong place, we correct.

Two: Realize

(1) atoi () function prototype: int atoi (char *str) header file Stdlib.h

Function Purpose: Converts a string to an integer value
Input parameters: str to be converted to integer number string
Return value: The converted value is returned successfully, and the failure returns 0.

(2) Code implementation

int My_atoi (char s[]) {    int i,n,sign;    For (I=0;isspace (S[i]); i++);   <strong>//Skip Blank </strong>,isspace () This function is in the type.h header file, or s[i]== ' implement sign=    (s[i]== '-')? -1:1;    if (s[i]== ' + ' | | s[i]== '-')     <strong>//skip sign bits </strong>        i++;    For (N=0;isdigit (S[i]); i++)        n=10*n+ (s[i]-' 0 ');       <strong>//converts numeric characters to shaping digital </strong>,<span style= "FONT-SIZE:13.3333339691162PX; Font-family:arial, Helvetica, Sans-serif; >isdigit () This function is in type.h header file, can also s[i]>= ' 0 ' && s[i]<= ' 9 ' to realize; <span style= "Font-family:consolas, ' Courier New ', Courier, mono, serif; font-size:12px; line-height:18px; Background-color:rgb (248, 248, 248); " >0X30 is ' 0 ' </span></span>    return sign*n;}
(3) prototype of the Itoa () function: char *itoa (int value, char *str,int radix)

function use: converts the value of an integer literal to a string
input parameter: value to be The integer number of the conversion ; STR
the address of the target string, which is the return value; Radix: The converted binary number, can be 10, 16 and so on.

Return value: A string was successfully returned.

(4) code implementation

/*converts an int, or long into a character string converts an integer to */char* my_itoa (int n,char str[]) {    int i,j,len,sign;    if ((sign=n) <0)    //record symbol        n=-n;         Make n a positive    i=0;    do{        str[i++]=n%10+ ' 0 ';    Remove a digital    }while ((n/=10) >0);      Cyclic Division    if (sign<0)        str[i++]= '-';    str[i]= ' + ';    Len = i;//for    (j=len-1,i=0;j>i;j--, i++)        //generated numbers are in reverse order, so swap    {        str[j] ^= str[i];        Str[i] ^= str[j];        STR[J] ^= str[i];    }    return str;}

(5) Main function test

#include "stdio.h" #include "ctype.h" #include "stdlib.h" int main () {int n;    Char Str[32],*ans;    Ans = My_itoa ( -123,STR);    printf ("Self-directed reshape-to-string function My_itoa ():%s%s\n", ANS,STR);    printf ("The system comes with an itoa-to-string function ():%s%s\n", Itoa ( -1,str,16), str);    printf ("self-directed string-shaping function My_atoi ():%d\n", My_atoi ("22QQQ"));    printf ("The system comes with the string-shaping function atoi ():%d\n", Atoi (" -2QQQ"));    System ("pause"); return 0;}

(6) Test Result:



Three: the shortcomings

(1) the prototype of the Itoa () function: char *itoa (int value, char *str,int radix), which is not fully implemented by itself, but simply implements the 10-binary

(2) The following will be improved, there are deficiencies, please teach the great God

(3) the prototype of the Itoa () function: char *itoa (int value, char *str,int radix), its own simple implementation, the default is to implement the 10 binary

Char __itoa[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',                ' a ', ' B ', ' C ', ' d ', ' e ', ' F '};const unsigned int my_max = 0xffffffffu;char* my_itoa2 (int n,char str[],int radix=10) {    int i,j,len,sign;    unsigned int tmp;    i = 0;    if (n<0)    //    {        tmp = My_max + n + 1;//This seems to be possible, in accordance with the method of taking the reverse plus one        do{            Str[i++]=__itoa[tmp%radix];    Remove a number        }while ((Tmp/=radix) >0);//Loop    divide    }    else {        do{            Str[i++]=__itoa[n%radix];    Take down a number        }while ((N/=radix) >0);//Cycle Division    }    str[i]= ' + ';    Len = i;//for    (j=len-1,i=0;j>i;j--, i++)        //generated numbers are in reverse order, so swap    {        str[j] ^= str[i];        Str[i] ^= str[j];        STR[J] ^= str[i];    }    return str;}


C + + implements Atoi () and Itoa () functions (String and Integer conversions)

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.