1. Brève introduction
En langage C, la conversion entre entiers et chaînes dispose de fonctions d'extension largement utilisées (bibliothèque non standard), et vous pouvez également essayer des implémentations simples vous-même.
2. Conversion d'entier en chaîne
1. Fonction d'extension itoa
itoa (signifiant integer to alphanumeric) est une fonction qui convertit un entier en chaîne de caractères.
Dans l'environnement Windows, dans le fichier d'en-tête <stdlib.h> :
char* itoa(int value,char*string,int radix);//value: 要转换的整数,string: 转换后的字符串,radix: 转换进制数,如2,8,10,16 进制等。
Code source de la fonction :
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;
}
Exemple de programme :
Exemple
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number1 = 123456;
int number2 = -123456;
char string[16] = {0};
itoa(number1,string,10);
printf("Nombre : %d, chaîne convertie : %s\n",number1,string);
itoa(number2,string,10);
printf("Nombre : %d, chaîne convertie : %s\n",number2,string);
return 0;
}
Capture d'écran du résultat :

2. Implémentation simple par vous-même
Code d'implémentation :
Exemple
#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("Nombre : %d, chaîne convertie : %s\n",number1,string);
Int2String(number2,string);
printf("Nombre : %d, chaîne convertie : %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;
}
Capture d'écran du résultat :
3. Chaîne en entier
1. Fonction d'extension
atoi (signifiant alphanumérique vers entier) est une fonction qui convertit une chaîne en entier.
Sous environnement Windows, dans le fichier d'en-tête <stdlib.h>.
int atoi(const char *nptr); // Fonction de conversion d'une chaîne en entier, nptr : la chaîne à convertir
Code source de la fonction
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;
}
Exemple de programme :
Exemple
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("String\"123456\"Convertir en nombre : %d\n",atoi("123456"));
printf("String\"-123456\"Convertir en nombre : %d\n",atoi("-123456"));
return 0;
}

2. Implémentation simple par vous-même
Code source de l'implémentation :
Exemple
#include<stdio.h>
int String2Int(char *str);
int main()
{
printf("String\"123456\"Convertir en nombre : %d\n",String2Int("123456"));
printf("String\"-123456\"Convertir en nombre : %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;
}
Capture d'écran :

4. Utilisation de la fonction sprintf() et de la fonction sscanf()
Entier vers chaîne
Code de test :
Exemple
#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("Nombre : %d, chaîne convertie : %s\n",number1,string);
Int2String(number2,string);
printf("Nombre : %d, chaîne convertie : %s\n",number2,string);
return 0;
}
char* Int2String(int num,char *str)
{
sprintf(str, "%d", num);
return str;
}
Résultat de l'exécution:

Chaîne vers entier
Code de test :
Exemple
#include<stdio.h>
int String2Int(char *str);
int main()
{
printf("String\"123456\"Convertir en nombre : %d\n",String2Int("123456"));
printf("String\"-123456\"Convertir en nombre : %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;
}
Résultat de l'exécution:

5. Annexe : table des codes ASCII (partielle)

Auteur : Genven_Liang
Article original : https://blog.csdn.net/nanfeibuyi/article/details/80811498