Java 程式如何將字串轉換為位元組陣列
這是我們的字串。
String str = "Asia is a continent!";
現在讓我們使用位元組陣列和 getBytes() 方法達成我們的目標。
byte[] byteVal = str.getBytes();
現在,如果我們獲取陣列的長度,它將返回如下所示的完整示例中的長度 −
示例
public class Demo {
public static void main(String args[]) {
String str = "Asia is a continent!";
System.out.println(str);
// converted to byte array
byte[] byteVal = str.getBytes();
// getting the length
System.out.println(byteVal.length);
}
}輸出
Asia is a continent! 20
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP