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

Source: Internet
Author: User
Tags function prototype sprintf

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

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 wrapper class, by char[] array conversion to an integer is not so simple, atoi () itoa () in the 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

intMy_atoi (Chars[]) {    inti,n,sign;  for(i=0; Isspace (S[i]); i++); <strong>//Skip Blank </strong>,isspace () This function is in the type.h header file, or you can s[i]== ' implement Sign= (s[i]=='-')?-1:1; if(s[i]=='+'|| s[i]=='-') <strong>//Skip Sign bit </strong>i++;  for(n=0; IsDigit (S[i]); i++) n=Ten*n+ (s[i]-'0'); <strong>//convert 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>    returnsign*N;}

(3) prototype of the Itoa () function: char *itoa (int value, char *str,int radix)

Purpose of Function: Converts the integer value to a string
Input parameters: Value of the integer
to be converted, the address of the STR target string, that is, the return value; Radix: The converted binary number, which 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 a string*/Char* MY_ITOA (intNCharstr[]) {    inti,j,len,sign; if((Sign=n) <0)//Record SymbolsN=-n;//make n a positive numberI=0;  Do{str[i++]=n%Ten+'0';//Remove a number} while((n/=Ten) >0);//Dividing loops    if(sign<0) Str[i++]='-'; Str[i]=' /'; Len= i;//     for(j=len-1, i=0; j>i;j--, i++)//The generated numbers are in reverse order, so swap{Str[j]^=Str[i]; Str[i]^=Str[j]; STR[J]^=Str[i]; }    returnstr;}

(5) Main function test

1#include"stdio.h"2#include"ctype.h"3#include"stdlib.h"4 intMain ()5 {6&nbsp; &nbsp;intN;7&nbsp; &nbsp;Charstr[ +],*ans;8&nbsp; &nbsp; Ans = my_itoa (-123, str);9&nbsp; &nbsp; printf"self-directed reshape-to-string function My_itoa ():%s%s\n", ans,str);Ten&nbsp; &nbsp; printf"The system comes with the shaping of the string function itoa ():%s%s\n", Itoa (-1Str -), str); One&nbsp; &nbsp; printf"self-directed string-to-reshape function my_atoi ():%d\n", My_atoi ("&NBSP;22QQQ")); A&nbsp; &nbsp; printf"The system comes with a string-shaping function atoi ():%d\n", Atoi ("&NBSP;-2QQQ")); -&nbsp; &nbsp; System"Pause"); -&nbsp; &nbsp;return 0; the}

(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

1 Char__itoa[] = {'0','1','2','3','4','5','6','7','8','9',2                 'a','b','C','D','e','F'};3 ConstUnsignedintMy_max =0xFFFFFFFFu;4 Char* MY_ITOA2 (intNCharStr[],intradix=Ten)5 {6     inti,j,len,sign;7Unsignedinttmp;8i =0;9     if(n<0)//Ten     { OneTMP = My_max + n +1;//it seems to be possible, in accordance with the method of taking the opposite plus one A          Do{ -Str[i++]=__itoa[tmp%radix];//Remove a number -} while((Tmp/=radix) >0);//Dividing loops the     } -     Else -     { -          Do{ +Str[i++]=__itoa[n%radix];//Remove a number -} while((N/=radix) >0);//Dividing loops +     } A  atstr[i]=' /'; -Len = i;// -      for(j=len-1, i=0; j>i;j--, i++)//The generated numbers are in reverse order, so swap -     { -STR[J] ^=Str[i]; -Str[i] ^=Str[j]; inSTR[J] ^=Str[i]; -     } to     returnstr; +}

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.