Converting between strings and integers in c language

In the c language, we often face the problem of converting strings to integers and converting integers to strings.

The functions to convert strings to integers are defined in stdlib.h:

int atoi (const char * str);

The atoi function skips preceding spaces in str and starts converting from the first digit. Below is my implementation:

 

int my_atoi(const char *str)
{  
    //argument check first!   
    assert (NULL != str);  
    int ret = 0, sign = 1;  
    //skip tab and space   
    for (; *str == ' '||*str == '\t'; ++str);  
    if (*str == '-')
	{sign = -1;}
    if (*str == '-' || *str == '+')
	{++str;}
    while (isdigit(*str))
	{  
        ret = ret * 10 + * str - '0';  
        ++str;  
    }  
    return sign * ret;  
}

A few points to note:

 

1. Parametric test. Always put it at the very beginning, in case the input is a pointer, never forget to check if it is a NULL pointer first;

2. The spaces at the beginning of the string (I also added tabs) should be skipped;

3. Symbols, don't forget the plus and minus signs may appear in the first character of the number string!

Converting numbers to strings is not defined in the c standard (itoa just defines it in some compilers). The reason why it is not defined in the c standard I think is that the conversion of numbers into strings can be done using the sprintf formatted output function. Of course we can write an itoa function ourselves. The parameter prototype here refers to itoa defined in vc:

 

#define ABS(cond) (cond>0?cond:-cond)   
char *my_itoa(int value, char *string, int radix)  
{  
	assert (string != NULL);  
	char tmp[32] = {'\0'};  
	int tmpval = ABS(value);  
	int i, j;  
	for (i = 0; i < 32; ++i)  
	{                  
		tmp[i] = (tmpval % radix) + '0';  
		tmpval = tmpval / radix;  
		if (tmpval == 0)  
			break;  
	}  
	if (value < 0)
	{tmp[++i] = '-';}  
	for (j = 0; i >= 0; --i)  
	{string[j++] = tmp[i];}  
	string[j] = '\0';  
	return string;  
}

Of course, in general, we can use sprintf to complete the task:

 

char num[32];
sprintf(num, "%d", 12345);

 Convert string to integer in c/c++, convert integer to string

#include <stdlib.h> //This is the header file
long atoi(char *); //This is to convert characters to integers
double atof(char *); //this is to convert floating point
char itoa(int i); // convert integer to string

------------------------------------------------------------------------------------------------------------------------

The source code of ideas written by itoa netizens, replace integers with characters, sign judges positive and negative numbers, but this

 

void itoa(int n, char s[])
{
	int i;
	int sign;
	if ((sign=n)<0)
	{n = -n;}

	i = 0;

	do
	{s[i++] = n % 10 + '0';}
	while ((n /= 10) > 0);

	if (sign<0)
	{s[i++] = '-';}

	s[i] = '\0';
	reverse(s); // Reverse S, not a function in C/C++? ? ? ?
}

There is another one, this function has 3 parameters, the first is the converted number, the second is the converted character, and the third is the base, but this function does not reverse the STRING

void my_itoa(int value ,char *string, int radix)
{
	char *psLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	while (value%radix != 0)
	{
		*string++ = psLetter[value%radix];
		value = value / radix;
	}
	*string = 0;
}

The following is a recursive algorithm to solve the reverse order problem. Available for testing on VC6.0.

#include <stdio.h>
#include <math.h>

char* my_itoa(int value, char * string, int radix)
{
	if (value < 0)
	{
		*string = '-'; 
		value = abs(value); 
		itoa(value, string+1, radix); 
		return string ; 
	} 

	if (value%radix == 0 && value/radix == 0) 
	{return string;}

	string = itoa(value/radix, string, radix); 
	if (value % radix >10) 
	{*string ='A'+value%radix-10;}
	else 
	{*string = '0' + value%radix;} 

	*(string+1) = NULL; 

	return string+1; 
} 

void main(void) 
{ 
	int a; 
	char s[25]; 
	a = -15; 
	my_itoa(a, s, 10); 
	printf("%s", s); 
	getchar(); 
}  
转自: http://www.cnblogs.com/GL-BBL/archive/2012/08/19/2646467.html
对代码进行了一点整理,方便阅读,若有错误,希望能够指出。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586517&siteId=291194637