Metoda Java String getBytes () codifică șirul într-o secvență de octeți și îl stochează într-o matrice de octeți.
Sintaxa getBytes()
metodei String este:
string.getBytes() string.getBytes(Charset charset) string.getBytes(String charsetName)
Aici, șirul este un obiect al String
clasei.
getBytes()
Metoda returneaza o matrice octet.
1. getBytes () Fără niciun parametru
Dacă nu treceți niciun parametru, getBytes()
codificați șirul utilizând setul de caractere implicit al platformei.
Exemplu: getBytes () Fără niciun parametru
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)); ) )
Ieșire
(74, 97, 118, 97)
Notă: Am folosit Arrays
clasa din exemplul de mai sus pentru a imprima matricea de octeți într-o formă lizibilă. Nu are nimic de-a face cu getBytes(
).
2. getBytes () Cu parametrul CharSet
Aici sunt CharSet
disponibile diferite în java:
- UTF-8 - Format de transformare UCS pe opt biți
- UTF-16 - Format de transformare UCS pe șaisprezece biți
- UTF-16BE - Format de transformare UCS pe șaisprezece biți, ordine de octeți big-endian
- UTF-16LE - Format de transformare UCS pe șaisprezece biți, ordin de octeți puțin endian
- US-ASCII - ASCII pe șapte biți
- ISO-8859-1 - Alfabetul latin ISO nr. 1
Exemplu: getBytes () Cu parametrul 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)); ) )
Ieșire
(74, 97, 118, 97) (-2, -1, 0, 74, 0, 97, 0, 118, 0, 97)
Notă: În programul de mai sus, am importat java.nio.charset.Charset
pentru utilizare CharSet
. Și am importat Arrays
clasa pentru a imprima matricea de octeți într-o formă lizibilă.
3. getBytes () With String Parameter
De asemenea, puteți specifica tipul de codificare pentru getBytes()
utilizarea șirurilor. Când utilizați getBytes()
acest mod, trebuie să înfășurați codul în interiorul try … catch block.
Exemplu: getBytes () With String Parameter
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"); ) ) )
Ieșire
(74, 97, 118, 97) (-2, -1, 0, 74, 0, 97, 0, 118, 0, 97) java.io.Excepție codificare excepțională: codarea UTF-34 este greșită
Notă: Am importat java.util.Arrays pentru a imprima matricea de octeți într-o formă lizibilă. Nu are nimic de-a face getBytes()
.