Chaîne Java getBytes ()

La méthode Java String getBytes () code la chaîne en une séquence d'octets et la stocke dans un tableau d'octets.

La syntaxe de la getBytes()méthode String est:

 string.getBytes() string.getBytes(Charset charset) string.getBytes(String charsetName)

Ici, string est un objet de la Stringclasse.

La getBytes()méthode renvoie un tableau d'octets.

1. getBytes () sans aucun paramètre

Si vous ne transmettez aucun paramètre, getBytes()encode la chaîne à l'aide du jeu de caractères par défaut de la plateforme.

Exemple: getBytes () sans aucun paramètre

 import java.util.Arrays; class Main ( public static void main(String() args) ( String str = "Java"; byte() byteArray; // convert the string to a byte array // using platform's default charset byteArray = str.getBytes(); System.out.println(Arrays.toString(byteArray)); ) )

Production

 (74, 97, 118, 97)

Remarque: Nous avons utilisé la Arraysclasse dans l'exemple ci-dessus pour imprimer le tableau d'octets sous une forme lisible. Cela n'a rien à voir avec getBytes().

2. getBytes () avec le paramètre CharSet

Voici différents CharSetdisponibles en java:

  • UTF-8 - Format de transformation UCS huit bits
  • UTF-16 - Format de transformation UCS 16 bits
  • UTF-16BE - Format de transformation UCS 16 bits, ordre des octets big-endian
  • UTF-16LE - Format de transformation UCS 16 bits, ordre des octets petit boutiste
  • US-ASCII - ASCII 7 bits
  • ISO-8859-1 - Alphabet latin ISO n ° 1

Exemple: getBytes () avec le paramètre CharSet

 import java.util.Arrays; import java.nio.charset.Charset; class Main ( public static void main(String() args) ( String str = "Java"; byte() byteArray; // using UTF-8 for encoding byteArray = str.getBytes(Charset.forName("UTF-8")); System.out.println(Arrays.toString(byteArray)); // using UTF-16 for encoding byteArray = str.getBytes(Charset.forName("UTF-16")); System.out.println(Arrays.toString(byteArray)); ) )

Production

 (74, 97, 118, 97) (-2, -1, 0, 74, 0, 97, 0, 118, 0, 97)

Remarque: dans le programme ci-dessus, nous avons importé java.nio.charset.Charsetpour utiliser CharSet. Et, nous avons importé la Arraysclasse pour imprimer le tableau d'octets sous une forme lisible.

3. getBytes () avec paramètre de chaîne

Vous pouvez également spécifier le type d'encodage à getBytes()utiliser des chaînes. Lorsque vous utilisez getBytes()de cette manière, vous devez envelopper le code dans le bloc try… catch.

Exemple: getBytes () avec un paramètre de chaîne

 import java.util.Arrays; class Main ( public static void main(String() args) ( String str = "Java"; byte() byteArray; try ( byteArray = str.getBytes("UTF-8"); System.out.println(Arrays.toString(byteArray)); byteArray = str.getBytes("UTF-16"); System.out.println(Arrays.toString(byteArray)); // wrong encoding // throws an exception byteArray = str.getBytes("UTF-34"); System.out.println(Arrays.toString(byteArray)); ) catch (Exception e) ( System.out.println(e + " encoding is wrong"); ) ) )

Production

 (74, 97, 118, 97) (-2, -1, 0, 74, 0, 97, 0, 118, 0, 97) java.io.UnsupportedEncodingException: l'encodage UTF-34 est incorrect

Remarque: Nous avons importé java.util.Arrays pour imprimer le tableau d'octets sous une forme lisible. Cela n'a rien à voir avec getBytes().

Articles intéressants...