赞
踩
//字节数组转字符串
byte[] data = {104, 101, 108, 108, 111};
//1.整体转换
//传递一个参数,即字节数组名
String str1 = new String(data);
System.out.println(str1);
//2.指定区间转换
//传递三个参数,即字节数组名,开始下标,转换长度
String str2 = new String(data, 1, 3);
System.out.println(str2);
hello
ell
//字符串转字节数组
String str = "hello";
//1.调用API
byte[] data1 = str.getBytes();
System.out.println(Arrays.toString(data1));
//2.遍历
byte[] data2 = new byte[str.length()];
for (int i = 0; i < str.length(); i++) {
data2[i] = (byte) str.charAt(i);
}
System.out.println(Arrays.toString(data2));
[104, 101, 108, 108, 111]
[104, 101, 108, 108, 111]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。