java 字元串 位元組數組
Today we will learn how to convert String to byte array in java. We will also learn how to convert byte array to String in Java.
今天,我們将學習如何在Java中将String轉換為位元組數組。 我們還将學習如何在Java中将位元組數組轉換為String。
字元串到位元組數組 (String to byte array)
We can use String class
getBytes()
method to encode the string into a sequence of bytes using the platform’s default charset. This method is overloaded and we can also pass
Charset
as argument.
我們可以使用String類的
getBytes()
方法使用平台的預設字元集将字元串編碼為位元組序列。 此方法已重載,我們也可以将
Charset
作為參數傳遞。
Here is a simple program showing how to convert String to byte array in java.
這是一個簡單的程式,顯示了如何在Java中将String轉換為位元組數組。
package com.journaldev.util;
import java.util.Arrays;
public class StringToByteArray {
public static void main(String[] args) {
String str = "PANKAJ";
byte[] byteArr = str.getBytes();
// print the byte[] elements
System.out.println("String to byte array: " + Arrays.toString(byteArr));
}
}
Below image shows the output when we run the above program.
下圖顯示了運作上述程式時的輸出。
We can also get the byte array using the below code.
我們還可以使用以下代碼擷取位元組數組。
byte[] byteArr = str.getBytes("UTF-8");
However if we provide Charset name, then we will have to either catch
UnsupportedEncodingException
exception or throw it. Better approach is to use
StandardCharsets
class introduced in Java 1.7 as shown below.
但是,如果提供了字元集名稱,則必須捕獲
UnsupportedEncodingException
異常或将其抛出。 更好的方法是使用Java 1.7中引入的
StandardCharsets
類,如下所示。
byte[] byteArr = str.getBytes(StandardCharsets.UTF_8);
That’s all the different ways to convert String to byte array in java.
在Java中,這是将String轉換為位元組數組的所有不同方法。
Java位元組數組到字元串 (Java byte array to String)
Let’s look at a simple program showing how to convert byte array to String in Java.
讓我們看一個簡單的程式,該程式顯示如何在Java中将位元組數組轉換為String。
package com.journaldev.util;
public class ByteArrayToString {
public static void main(String[] args) {
byte[] byteArray = { 'P', 'A', 'N', 'K', 'A', 'J' };
byte[] byteArray1 = { 80, 65, 78, 75, 65, 74 };
String str = new String(byteArray);
String str1 = new String(byteArray1);
System.out.println(str);
System.out.println(str1);
}
}
Below image shows the output produced by the above program.
下圖顯示了以上程式産生的輸出。
Did you notice that I am providing char while creating the byte array?
您是否注意到建立位元組數組時提供了char?
It works because of autoboxing and char ‘P’ is being converted to 80 in the byte array. That’s why the output is the same for both the byte array to string conversion.
由于自動裝箱而起作用,并且char'P'在位元組數組中被轉換為80。 這就是為什麼位元組數組到字元串轉換的輸出相同的原因。
String also has a constructor where we can provide byte array and Charset as an argument. So below code can also be used to convert byte array to String in Java.
字元串還有一個構造函數,我們可以在其中提供位元組數組和Charset作為參數。 是以,以下代碼也可以用于在Java中将位元組數組轉換為String。
String str = new String(byteArray, StandardCharsets.UTF_8);
String class also has a method to convert a subset of the byte array to String.
String類還具有一種将位元組數組的子集轉換為String的方法。
byte[] byteArray1 = { 80, 65, 78, 75, 65, 74 };
String str = new String(byteArray1, 0, 3, StandardCharsets.UTF_8);
Above code is perfectly fine and ‘str’ value will be ‘PAN’. That’s all about converting byte array to String in Java.
上面的代碼非常好,“ str”值為“ PAN”。 這就是在Java中将位元組數組轉換為String的全部内容。
GitHub Repository. GitHub存儲庫中簽出更多數組示例。
Reference: getBytes API Doc
參考: getBytes API文檔
翻譯自: https://www.journaldev.com/770/string-byte-array-java
java 字元串 位元組數組