1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static String convert(String hexa) {
// On vérifie la longeur de la chaine :
if (hexa.length() % 4 != 0) {
throw new RuntimeException("Taille incorrecte");
}
// Création du buffer de lecture :
StringBuffer buf = new StringBuffer();
// On parcours la chaine par bloc de 4 caractères :
for (int pos = 0; pos<hexa.length(); pos+=4) {
// On récupère la chaine courante :
String substring = hexa.substring(pos, pos+4);
// Que l'on convertit en int puis en char :
char c = (char) Integer.parseInt(substring, 16);
// Et on ajoute le char au buffer :
buf.append(c);
}
return buf.toString();
} |
Partager