请教中文转换的问题

shinya 2005-03-18 03:07:58
String value = "测试";
byte[] temp = value.getByte("ISO8859-1");

请问再如何转换将temp变为中文的"测试"?
new String(temp,"GBK");是不行的,只有value为英文才行或者
改为byte[] temp = value.getByte();
...全文
234 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
OnlyFor_love 2005-03-23
  • 打赏
  • 举报
回复
同意楼上,在jdk文档中写的很清楚了!
faen 2005-03-23
  • 打赏
  • 举报
回复
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.
你看文档的解释:当string不能被编码的给定的字符集的时候,这个方法的行为是未知的。


shinya 2005-03-20
  • 打赏
  • 举报
回复
自己顶
topil 2005-03-19
  • 打赏
  • 举报
回复
关注
shinya 2005-03-19
  • 打赏
  • 举报
回复
有人会吗??急呀
shinya 2005-03-18
  • 打赏
  • 举报
回复
嗯,我先转成了"ISO8859-1",在new String以前至少应该是"GBK"的byte数组
如何数组转数组?就是这个问题
asa516 2005-03-18
  • 打赏
  • 举报
回复
to shinya(逸风):
你的意思是把中文字符串转换成字节数组吗?
shinya 2005-03-18
  • 打赏
  • 举报
回复
各位,你们把问题再好好看一下,这么简单我也不好意思问了
kill8108 2005-03-18
  • 打赏
  • 举报
回复
以上楼主都行啦!是的,应多查JDK文档
fogs 2005-03-18
  • 打赏
  • 举报
回复
回复人: josy(风尘浪子) ( ) 信誉:98 2005-03-18 08:59:00 得分: 0


private String ISO2Gb_W(String str) {
try {
return new String(str.getBytes("GBK"),"8859_1");
}catch(Exception ex) {}
return "null";
}
--------------------------------------------------
楼上正解,建议楼主多看JDK的文档
cuilichen 2005-03-18
  • 打赏
  • 举报
回复
呵呵,学到了
百年树人 2005-03-18
  • 打赏
  • 举报
回复
private String ISO2Gb_W(String str) {
try {
return new String(str.getBytes("GBK"),"8859_1");
}catch(Exception ex) {}
return "null";
}
shinya 2005-03-18
  • 打赏
  • 举报
回复
感觉楼上说得有道理,不过应该可以转转中文,通过unicode中转
faen 2005-03-18
  • 打赏
  • 举报
回复
我的一点观点,不知道正确否。
String value = "测试";
byte[] temp = value.getByte("ISO8859-1");
这两句执行完后,temp数组的长度是2个字节。
因为value占四个字节,你转换的时候已经有信息丢失了。
靠temp数组已经不能转回来了吧
shuren 2005-03-18
  • 打赏
  • 举报
回复
兄弟写个字符编码过滤器吧!以后不管在该工程中任何地方都可以用了!

62,614

社区成员

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

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