Tom's Guide > Forum > Programmation > [Langage C] Convertir un entier en caractère

[Langage C] Convertir un entier en caractère

Forum Programmation : [Langage C] Convertir un entier en caractère

TomsGuide.com : 800 000 inscrits répondent à toutes vos questions high-tech et informatique. Pour obtenir de l'aide, inscrivez-vous gratuitement !
Mot :    Pseudo :           
 

Bonjour,

Quelqu'un a une idée pour convertir un entier (int) en caractère de manière à pouvoir le concaténer à une chaîne de caractère via la fonction strcat() ?

Merci,
dachu

Liens sponsorisés
Inscrivez-vous ou connectez-vous pour masquer ceci.

Code :
  1. Reformatting atof(3), please wait...
  2. ATOF(3)                              Linux Programmer's Manual                              ATOF(3)
  3. NAME
  4.       atof - convert a string to a double.
  5. SYNOPSIS
  6.       #include <stdlib.h>
  7.       double atof(const char *nptr);
  8. DESCRIPTION
  9.       The atof() function converts the initial portion of the string pointed to by nptr to double.
  10.       The behaviour is the same as
  11.               strtod(nptr, (char **)NULL);
  12.       except that atof() does not detect errors.
  13. RETURN VALUE
  14.       The converted value.
  15. CONFORMING TO
  16.       SVID 3, POSIX, BSD 4.3, ISO 9899
  17. SEE ALSO
  18.       atoi(3), atol(3), strtod(3), strtol(3), strtoul(3)
  19. GNU                                          1993-03-29                                    ATOF(3)

Répondre à lissyx

Ca ne fonctionne pas :-( ! Ca crée un bug quand il lance la fonction...

Je vous la montre :

--------------------------------
....
char adresse[30] = "" ;
char nbr ;
temp = classement->courant ;
strcat(adresse,FICH_DOSSIER) ;
strcat(adresse,"/" ) ;
strcat(adresse,FICH_PREF_COMPET) ;
strtod(competition.id, nbr);
//strcat(adresse,competition.id) ;
strcat(adresse,"." ) ;
strcat(adresse,FICH_EXT) ;
fich = fopen(adresse, "wb" ) ;
....
--------------------------------

Répondre à dachu

Code :
  1. [asmodaiii@phoenix:~] $ cat test.c
  2. #include <stdlib.h>
  3. char    *test(int nb)
  4. {
  5.   int  i;
  6.   int  cpt;
  7.   char  *res;
  8.  
  9.   for (i = 1, cpt = 1; nb / i >= 10; i *= 10, cpt++)
  10.     ;
  11.   res = malloc(sizeof(*res) * (cpt + 1));
  12.   for (cpt = 0; i; cpt++, i /= 10)
  13.     res[cpt] = (nb / i) % 10 + '0';
  14.   res[cpt] = '\0';
  15.   return (res);
  16. }
  17. int    main()
  18. {
  19.   printf("%s\n", test(4321));
  20. }

Répondre à AsMoDaiii

Merci à tous !

J'y suis arrivé autrement car je n'y arrivais pas avec vos fonctions... Ca compliait, mais ca me faisait une erreur d'adresse mémoire !

J'ai utilisé une fonction de la bibliothèque en faisant :

---------------------------------------------------
char *nbr=(char *)malloc(sizeof(char) * 10) ;
itoa(competition.id,nbr,10) ;
---------------------------------------------------

Encore merci à tous !
dachu

Répondre à dachu
Tom's Guide > Forum > Programmation > [Langage C] Convertir un entier en caractère
Aller à :

Il y a 1675 utilisateurs connus et inconnus. Pour voir la liste des connectés connus, cliquez ici.

Attention

Vous allez répondre sur un sujet resté inactif pendant plus de 6 mois.
Assurez-vous d'apporter des éléments nouveaux à la discussion avant de poursuivre.

Répondre Annuler
Liens