黑马程序员技术交流社区

标题: 怎么将string字符串转换成byte[ ]数组 [打印本页]

作者: dagouxiong    时间: 2015-11-22 20:40
标题: 怎么将string字符串转换成byte[ ]数组
怎么将string字符串转换成byte[ ]数组
在线求大神帮助,谢谢谢谢

作者: 王先森    时间: 2015-11-22 20:40
//string 转 byte[]

String str = "Hello";

byte[] srtbyte = str.getBytes();

// byte[] 转 string

String res = new String(srtbyte);

System.out.println(res);
作者: 袁有福123    时间: 2015-11-22 21:11
1、string 转 byte[]

String str = "Hello";byte[] srtbyte = str.getBytes();
2、byte[] 转 string

byte[] srtbyte;String res = new String(srtbyte);System.out.println(res);
3、设定编码方式相互转换

String str = "hello";byte[] srtbyte = null;try {    srtbyte = str.getBytes("UTF-8");    String res = new String(srtbyte,"UTF-8");    System.out.println(res);} catch (UnsupportedEncodingException e) {    // TODO Auto-generated catch block    e.printStackTrace();}
作者: 袁有福123    时间: 2015-11-22 21:15
不知道能不能帮到你  如果帮到了 请给我黑马币 这个对我很重要的!谢谢啦!
作者: 大凶之兆    时间: 2015-11-22 21:45
告诉你一点   如果不知道用什么方法,那就根据你知道的相关的类查API    字符串转byte数组 ,那就应该查String类的方法
getBytes
public byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

我们不知道的方法是很多的  可以练练API   对自己有很大的帮助的
作者: 天晴了吗    时间: 2015-11-24 23:36
String类里不是有个方法吗.. byte[] getBytes()
作者: 谁的苍茫    时间: 2016-1-31 01:35
如果有不懂的地方多翻一下API,会给你意想不到的答案,这样子会加深你的印象
作者: 1191065242    时间: 2016-5-1 16:07
String str = "adasad";
byte[] bt = str.getBytes();




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2