Communauté Eggdrop
fonction str2hex [résolu] - Version imprimable

+- Communauté Eggdrop (https://forum.eggdrop.fr)
+-- Forum : Eggdrop et TCL (https://forum.eggdrop.fr/forumdisplay.php?fid=8)
+--- Forum : Scripts TCL (https://forum.eggdrop.fr/forumdisplay.php?fid=4)
+--- Sujet : fonction str2hex [résolu] (/showthread.php?tid=404)



fonction str2hex [résolu] - MenzAgitat - 25/05/2009

J'ai besoin d'une fonction pour convertir une chaîne de caractères en hexadécimal (ou en octal, je m'accomoderai de l'un comme de l'autre) et je sèche. Des idées ?


RE: fonction str2hex / str2oct - CrazyCat - 25/05/2009

Avec les commandes scan et format non ?
tcl
proc str2hex { str } {
   set hex ''
   while { [scan $str %c letter] != -1 } {
      append hex [format %x $letter]
   }
   return $hex
}
proc str2oct { str } {
   set oct ''
   while { [scan $str %c letter] != -1 } {
      append oct [format %o $letter]
   }
   return $oct
}


Je ne suis vraiment pas certain de mon coup, mais c'est peut-être une piste.


RE: fonction str2hex / str2oct - MenzAgitat - 25/05/2009

J'avais déjà essayé un truc comme ça mais format %x attend un entier et non un caractère.

En plus, à vue de nez y'a rien qui fait avancer le while et ça va faire une boucle infinie.


Edit : j'ai trouvé la solution du côté de binary :
tcl
proc str2hex { str } {
 binary scan $str H* hex
 return hex
}





RE: fonction str2hex / str2oct - CrazyCat - 25/05/2009

Ah oui, j'ai zappé un substring Smile

Il faudrait donc convertir ta chaine en valeurs ascii non ?
tcl
proc asc { char } {
   return [ scan $char "%c" ]
}
proc chr { num } {
   return [ format "%c" $num ]
}


Je ne vois pas tellement comment convertir 'toto' en hexa sinon.


RE: fonction str2hex [résolu] - MenzAgitat - 25/05/2009

J'ai trouvé la solution et édité mon message 2mn avant que tu ne valides ta réponse :p