关于byte[]向String转换的问题

yangfeitarena 2009-07-22 08:25:15
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bs = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String s = new String(bs,3,4);
System.out.println(s);
}
打印出来怎么是乱码啊?如何打印出4567
...全文
86 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mianfeidog 2009-07-23
  • 打赏
  • 举报
回复
学习。
冰思雨 2009-07-23
  • 打赏
  • 举报
回复
顶2楼的。

所有显示的字符,都是要编码滴。不编码或者编码错误,都有可能被显示成乱码
ouyangyh 2009-07-22
  • 打赏
  • 举报
回复
byte字节数组转码,会两个byte转成一个字符
BurnedVanilla 2009-07-22
  • 打赏
  • 举报
回复
2楼正解
rumlee 2009-07-22
  • 打赏
  • 举报
回复
byte[] bs = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
改成
byte[] bs = new byte[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
因为你那样写的不是字符'1',而是ascii为1的字符,所以打印出来是乱码。
clihean 2009-07-22
  • 打赏
  • 举报
回复
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bs = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56, 57 };
String s = new String(bs, 3, 4);
System.out.println(s);
}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧