Java中如何实现String字符串与其他数据类型的转换?
- 内容介绍
- 文章标签
- 相关推荐
本文共计137个文字,预计阅读时间需要1分钟。
1. 使用1 byte数组与String字符串相互转换 1-1. 创建一个String对象str,并赋值为Hello 1-2. 将str转换为byte数组srtbyte 1-3. 将byte数组srtbyte转换回String 1-4. 输出转换后的字符串res
1 byte数组和String字符串
1-1 string 转 byte[]
String str = "Hello";byte[] srtbyte = str.getBytes();
1-2 byte[] 转 string
byte[] srtbyte;String res = new String(srtbyte);
System.out.println(res);
1-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
本文共计137个文字,预计阅读时间需要1分钟。
1. 使用1 byte数组与String字符串相互转换 1-1. 创建一个String对象str,并赋值为Hello 1-2. 将str转换为byte数组srtbyte 1-3. 将byte数组srtbyte转换回String 1-4. 输出转换后的字符串res
1 byte数组和String字符串
1-1 string 转 byte[]
String str = "Hello";byte[] srtbyte = str.getBytes();
1-2 byte[] 转 string
byte[] srtbyte;String res = new String(srtbyte);
System.out.println(res);
1-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

