为何一样的程序在WINDOWS和LINUX下的结果却不一样?!!!

MAX°孟兆 2005-08-04 10:50:02
这是程序的代码,主要是getBytes()的处理在不同的平台会有不同的结果:

String s1 = "测试Test";
String s2 = null;
byte[] b = null;
try
{
b = s1.getBytes("iso-10646-ucs-2");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
System.out.println();

s2 = new String(b, "iso-10646-ucs-2");
b = s2.getBytes();
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
System.out.println();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
...全文
231 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
MAX°孟兆 2005-08-07
  • 打赏
  • 举报
回复
我的程序是在WIN32下写,然后拿到LINUX下运行的.
楼上的方法我都试过了,还是不行.
有没有设置JVM运行期间使用的默认字符编码的方法或是其它方法啊?
楼上那些方法你们能通过吗?
liuqstion 2005-08-05
  • 打赏
  • 举报
回复
呵呵,编译的时候加一个encode参数不就可以了?
deWolf 2005-08-05
  • 打赏
  • 举报
回复
跟系统没关系,跟你的源文件编码有关。

你用默认的 ANSI 编码编写 *.java ,汉字在 win32下边用gb_2312编码

但是汉字在redhat 9.0+ fedora 1+ 的系统中全部使用 UTF-8 -16 -32编码的,

楼主大概在win下边写了源码 去linux跑了吧?

-----------------------------------------

解决办法: 1 在win32写代码,使用 notepad++, ultraEdit, 甚至notepad都可以,但是一定要转换为UTF保存 ,save as ... UTF-8 或者 -16
2 去linux 下编写好,来win32跑就好了。
shenpipi 2005-08-05
  • 打赏
  • 举报
回复
不同意楼上
public byte[] getBytes()
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
什么是the platform's default charset?
就算s2.getBytes()这些函数只是调用jvm的参数,那谁又能保证各个os下的jvm具有相同的参数?
congliu 2005-08-05
  • 打赏
  • 举报
回复
s2.getBytes()这些函数只是调用jvm的参数,不应该出现不同的。 楼主试试下载新版的jre
secondflying 2005-08-05
  • 打赏
  • 举报
回复
字符集编码不同吧,像二楼说的!
okjohn 2005-08-05
  • 打赏
  • 举报
回复
b = s2.getBytes(); //取得字符串s2在当前系统中默认字符集编码
shenpipi 2005-08-05
  • 打赏
  • 举报
回复
b = s2.getBytes();应该是这句的原因.
coolyulei_ 2005-08-05
  • 打赏
  • 举报
回复
有可能是不同的系统在字符编码上的问题

62,614

社区成员

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

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