1. Brief Introduction
In C language, the conversion between integers and strings has widely used extension functions (non-standard library), and you can also try simple implementations yourself.
2. Integer to String
1. Extension function itoa
itoa (meaning integer to alphanumeric) is a function that converts an integer to a string.
Under Windows environment, in the <stdlib.h> header file:
char* itoa(int value,char*string,int radix);//value: 要转换的整数,string: 转换后的字符串,radix: 转换进制数,如2,8,10,16 进制等。
Function source code:
char* itoa(int num,char* str,int radix)
{
char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned unum;
int i=0,j,k;
if(radix==10&&num<0)
{
unum=(unsigned)-num;
str[i++]='-';
}
else unum=(unsigned)num;
do
{
str[i++]=index[unum%(unsigned)radix];
unum/=radix;
}while(unum);
str[i]='\0';
if(str[0]=='-') k=1;
else k=0;
char temp;
for(j=k;j<=(i-1)/2;j++)
{
temp=str[j];
str[j]=str[i-1+k-j];
str[i-1+k-j]=temp;
}
return str;
}
Example program:
Example
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number1 = 123456;
int number2 = -123456;
char string[16] = {0};
itoa(number1,string,10);
printf("Number: %d, converted string: %s\n",number1,string);
itoa(number2,string,10);
printf("Number: %d, converted string: %s\n",number2,string);
return 0;
}
Effect screenshot:

2. Simple implementation by yourself
Implementation code:
Example
#include <stdio.h>
char* Int2String(int num,char *str);
int main()
{
int number1 = 123456;
int number2 = -123456;
char string[16] = {0};
Int2String(number1,string);
printf("Number: %d, converted string: %s\n",number1,string);
Int2String(number2,string);
printf("Number: %d, converted string: %s\n",number2,string);
return 0;
}
char* Int2String(int num,char *str)
{
int i = 0;
if(num<0)
{
num = -num;
str[i++] = '-';
}
do
{
str[i++] = num%10+48;
num /= 10;
}while(num);
str[i] = '\0';
int j = 0;
if(str[0]=='-')
{
j = 1;
++i;
}
for(;j<i/2;j++)
{
str[j] = str[j] + str[i-1-j];
str[i-1-j] = str[j] - str[i-1-j];
str[j] = str[j] - str[i-1-j];
}
return str;
}
Effect screenshot:
3. String to Integer
1. Extension function
atoi (meaning alphanumeric to integer) is a function that converts a string to an integer.
Under Windows environment, in the <stdlib.h> header file.
int atoi(const char *nptr); // String to integer function, nptr: the string to be converted
Function source code
int atoi(const char *nptr)
{
return (int)atol(nptr);
}
long atol(const char *nptr)
{
int c;
long total;
int sign;
while ( isspace((int)(unsigned char)*nptr) )
++nptr;
c = (int)(unsigned char)*nptr++;
sign = c;
if (c == '-' || c == '+')
c = (int)(unsigned char)*nptr++;
total = 0;
while (isdigit(c)) {
total = 10 * total + (c - '0');
c = (int)(unsigned char)*nptr++;
}
if (sign == '-')
return -total;
else
return total;
}
Example program:
Example
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("String\"123456\"Convert to number: %d\n",atoi("123456"));
printf("String\"-123456\"Convert to number: %d\n",atoi("-123456"));
return 0;
}

2. Simple implementation by yourself
Implementation source code:
Example
#include<stdio.h>
int String2Int(char *str);
int main()
{
printf("String\"123456\"Convert to number: %d\n",String2Int("123456"));
printf("String\"-123456\"Convert to number: %d\n",String2Int("-123456"));
return 0;
}
int String2Int(char *str)
{
char flag = '+';
long res = 0;
if(*str=='-')
{
++str;
flag = '-';
}
while(*str>=48 && *str<=57)
{
res = 10*res+ *str++-48;
}
if(flag == '-')
{
res = -res;
}
return (int)res;
}
Screenshot:

4. Using sprintf() function and sscanf() function
Integer to string
Test code:
Example
#include <stdio.h>
char* Int2String(int num,char *str);
int main()
{
int number1 = 123456;
int number2 = -123456;
char string[16] = {0};
Int2String(number1,string);
printf("Number: %d, converted string: %s\n",number1,string);
Int2String(number2,string);
printf("Number: %d, converted string: %s\n",number2,string);
return 0;
}
char* Int2String(int num,char *str)
{
sprintf(str, "%d", num);
return str;
}
Running result:

String to integer
Test code:
Example
#include<stdio.h>
int String2Int(char *str);
int main()
{
printf("String\"123456\"Convert to number: %d\n",String2Int("123456"));
printf("String\"-123456\"Convert to number: %d\n",String2Int("-123456"));
return 0;
}
int String2Int(char *str)
{
char flag = '+';
long res = 0;
if(*str=='-')
{
++str;
flag = '-';
}
sscanf(str, "%ld", &res);
if(flag == '-')
{
res = -res;
}
return (int)res;
}
Running result:

5. Appendix: ASCII code table (partial)

Author: Genven_Liang
Original article: https://blog.csdn.net/nanfeibuyi/article/details/80811498