Java 程序,如何将字符串中的字符作为字节数组获取
要将字符串中的字符作为字节数组获取,请使用 getBytes() 方法。
假设我们有以下字符串。
String str = "Demo Text!";
将它转换为字节数组。
byte[] arr = str.getBytes();
示例
public class Demo {
public static void main(String[] args) {
String str = "Demo Text!";
// getting as an array of bytes
byte[] arr = str.getBytes();
// displaying each character as a Byte value
for(byte res: arr) {
System.out.println(res);
}
}
}输出
68 101 109 111 32 84 101 120 116 33
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP