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