C ++ implements atoi () and itoa () functions (string and Integer Conversion), atoiitoa

Source: Internet
Author: User

C ++ implements atoi () and itoa () functions (string and Integer Conversion), atoiitoa

I. Cause

(1) Whether the String type is converted to the Integer type or the String type is converted to the Double type, which has very good internal functions in java and is very easy;

(2) but there are no Integer Double or other packaging classes in c. Converting from char [] array to Integer type is not that easy. atoi () itoa () is under widows, but it seems that there is no itoa () function in linux on the Internet. It's better to use sprintf,But I tested sprintf () sscanf () very inefficient.

(3) So I manually implemented the atoi () (string to integer) itoa (integer to string) two functions. If there is anything wrong with it, let us know.

II. Implementation

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

Function purpose: convert a string into an integer.
Input parameter: str string to be converted to an integer
Return Value: The converted value is returned. If the conversion fails, 0 is returned.

(2) code implementation

Int my_atoi (char s []) {int I, n, sign; for (I = 0; isspace (s [I]); I ++); // skip the blank, the isspace () function is in type. in the h header file, can the s [I] = ''implement sign = (s [I] = '-')? -; If (s [I] = '+' | s [I] = '-') // skip the symbol I ++; for (n = 0; isdigit (s [I]); I ++) n = 10 * n + (s [I]-'0'); // converts a number to an integer, the isdigit () function is in type. in the h header file, you can also implement the s [I]> = '0' & s [I] <= '9''; 0x30 is '0' return sign * n ;}
(3) prototype of the itoa () function: char * itoa (int value, char * str, int radix)

Function purpose: convert an integer value into a string.
Input parameter: the number of integer values to be converted; str
The address of the target string, that is, the return value. radix: the number of hexadecimal values after conversion, which can be decimal or hexadecimal.

Return Value: A string is returned successfully.

(4) code implementation

/* Converts an int or long into a character string Converts an integer to a string */char * my_itoa (int n, char str []) {int I, j, len, sign; if (sign = n) <0) // record symbol n =-n; // make n a positive number I = 0; do {str [I ++] = n % 10 + '0'; // take the next number} while (n/= 10)> 0 ); // cyclically divide if (sign <0) str [I ++] = '-'; str [I] = '\ 0'; len = I; // for (j = len-1, I = 0; j> I; j --, I ++) // The generated number is in reverse order, therefore, {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-edited integer-to-string function my_itoa (): % s \ n", ans, str); printf ("the built-in integer-to-string function itoa (): % s \ n ", itoa (-1, str, 16), str); printf (" self-compiled string to integer function my_atoi (): % d \ n ", my_atoi (" 22qqq "); printf (" the built-in string to integer function atoi (): % d \ n ", atoi ("-2qqq"); system ("pause"); return 0 ;}

(6) test results:



Iii. Weaknesses

(1) prototype of the itoa () function: char * itoa (int value, char * str, int radix), which is not fully implemented by itself, but simply implemented in decimal format.

(2) The following will be improved. There are some shortcomings on it. Please kindly advise.

(3) the prototype of the itoa () function: char * itoa (int value, char * str, int radix). It is implemented in a simple way by default in decimal format.

Char _ itoa [] = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F '}; const unsigned int MY_MAX = 0 xffffffu; 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; // It seems OK, do {str [I ++] =__ itoa [tmp % radix] According to the reversed addition method; // take the next number} while (tmp/= radix)> 0); // cyclic division} else {do {str [I ++] =__ itoa [n % radix]; // take the next digit} while (n/= radix)> 0); // returns the cyclic phase} str [I] = '\ 0'; len = I; // for (j = len-1, I = 0; j> I; j --, I ++) // The generated number is in reverse order, therefore, {str [j] ^ = str [I]; str [I] ^ = str [j]; str [j] ^ = str [I];} return str ;}


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.