comment convertir un tableau Byte [] en chaîne en Java
Dans certains cas, nous devons convertir la variable String en un format de tableau d'octets, par exemple, le chiffrement JCE. Mais comment pouvons-nousconvert a Byte[] array to String par la suite?
La fonction toString () simple comme le code suivant ne fonctionne pas. Il n'affichera pas le texte d'origine mais la valeur d'octet.
String s = bytes.toString();
Afin de convertir correctement le tableau Byte au format String, nous devons créer explicitement un objet String et lui affecter le tableau Byte.
String s = new String(bytes);
public class TestByte
{
public static void main(String[] argv) {
String example = "This is an example";
byte[] bytes = example.getBytes();
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = new String(bytes);
System.out.println("Text Decryted : " + s);
}
}
Sortie
Text : This is an example Text [Byte Format] : [B@187aeca Text [Byte Format] : [B@187aeca Text Decryted : This is an example