Convertion hexa to decim

Convertion hexa to decim - C - Programmation

Marsh Posté le 26-10-2005 à 21:05:49    

Bonjour,    
   
Je veus créer un prog. en C permettant d'afficher un base 10 , un nombre exprimé en héxadécimal passé en argument de la ligne de commande. Par exemple la commande ./ hexa2int 1a affichera : 26  
 
voila ce que j'ai fais, mais ceci ne fonctionne pas :
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int main(int argc, char** argv)
  5. {
  6. int d=0;
  7. if (argc>1)
  8. {
  9. printf("l'argument entrée en hexa est : [%s]\n",argv[1]);
  10. d = strtoul(argv[1], 16);
  11. printf("la somme de l'argument en base 10 est : [%d]\n", d);
  12. }
  13. return 0;
  14. }


 
Je comprend pas pourquoi
 
Merci
 

Reply

Marsh Posté le 26-10-2005 à 21:05:49   

Reply

Marsh Posté le 26-10-2005 à 21:16:44    

man strtoul
 
mauvais usage, ça doit hurler à la cmpilation

Reply

Marsh Posté le 26-10-2005 à 21:20:33    

math.h est inutile
 
Le prototype de strtoul est :

unsigned long  
strtoul(const char * restrict nptr, char ** restrict endptr, int base);


 
Tu dois donc l'utiliser ainsi :

strtoul(argv[1], NULL, 16);


En vérifiant que strtoul ne retourne pas 0, avec errno mis à EINVAL, si le paramètre fourni est incorrect.
 
 
Et pour printf(), c'est pas %d (entier), mais %lu (long unsigned) qu'il faut utiliser.

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed