Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1580 connectés 

  FORUM HardWare.fr
  Programmation
  C

  problème conversion Chaine->hexa

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

problème conversion Chaine->hexa

n°635562
AnG-L
Posté le 09-02-2004 à 00:22:26  profilanswer
 

bonjour :)
 
mon problème est certainement simple, j'ai déjà scrutté le forum et j'ai rien trouvé
 
en fait il s'agirait de transformer une chaine type
=> "5a258b"
en son code hexa 0x5a 0x25 0x8b
 
voila si qqn a une idée merci :)

mood
Publicité
Posté le 09-02-2004 à 00:22:26  profilanswer
 

n°635598
matafan
Posté le 09-02-2004 à 02:48:44  profilanswer
 

sscanf avec %x ou bien strtol/strtoll.

n°635601
printf
Baston !
Posté le 09-02-2004 à 03:15:42  profilanswer
 

Une méthode de barbare, mais si c'est pour un TP ça devrait passer :D
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /* retourne le nombre (base 10) correspondant a un chiffre hexadecimal */
  5. int cHex_to_iDec(char hex)      /* on suppose l'utilisation de l'ASCII */
  6. {
  7.   if ((hex>=48)&&(hex<=57))  return (hex-48);
  8.   if ((hex>=65)&&(hex<=70))  return (hex-55);
  9.   if ((hex>=97)&&(hex<=102)) return (hex-87);
  10.   return -1; /* erreur si hex n'est pas un chiffre hexadecimal */
  11. }
  12. void Str_to_Dec(char *str, int *dec, int lg_dec)
  13. {
  14.   int i;
  15.   for(i=0; i<lg_dec; i++)
  16.     dec[i] = 16*cHex_to_iDec(str[i*2]) + cHex_to_iDec(str[i*2+1]);
  17. }
  18. void main()
  19. {
  20.   int i, *dec, lg_dec;
  21.   char *str = "5a258b";
  22.   lg_dec = strlen(str)/2;
  23.   dec = malloc(4*lg_dec);
  24.   Str_to_Dec(str, dec, lg_dec);
  25.   for(i=0; i<lg_dec; i++) printf("%d : %x\n", i, dec[i]);
  26. }


---------------
Un matin je me lèverai et il fera beau.
n°635670
AnG-L
Posté le 09-02-2004 à 10:03:10  profilanswer
 

merci les gars :)


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  problème conversion Chaine->hexa

 

Sujets relatifs
Probleme sql (configuration)Direct Sound : probleme de lecture
Problème de sockets ouvertsPetit probleme (apres compilation)
problème d'antislash à la création d'un fichier via un fichier php[C] Remplacer une partie d'une chaîne de caractères
problème de compilation(scite)problème avec les library VBa access 2000
[ASP+VBSCRIPT] Conversion d'une date (2/9/2004 --> 02/09/2004)Problème de maths
Plus de sujets relatifs à : problème conversion Chaine->hexa


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR