帮看一个程式

feptest 2009-11-25 02:21:42
程式如下:

public static void main(String args[]) throws UnsupportedEncodingException{

// System.out.println(Charset.defaultCharset());
byte[] bytes = new byte[256];
for(int i =0;i< 256;i++){
bytes[i] = (byte)i;
}
String s = new String(bytes);
for(int i =0, n= s.length();i<n ; i++)
System.out.print((int)s.charAt(i)+" ");
}


以上程式打出来是乱码。。
解决方法是要把 String s = new String(bytes); 改成
String s = new String(bytes,"ISO-8859-1");
我系统编码是GBK ,为什么用GBK字符集构造这个字符串会乱码???GBK不是兼容ASCII 的吗??

求解?!
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
saiche05 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 scarecrow_ss 的回复:]
Java codeimport java.net.*;import java.io.*;import java.nio.charset.Charset;publicclass Test{publicstaticvoid main(String args[])throws UnsupportedEncodingException{
System.out.println(Charset.defaultCharset().name());byte[] bytes=newbyte[256];for(int i=0;i<256;i++){
bytes[i]= (byte)i;
}
String s=new String(bytes);for(int i=0, n= s.length();i<n ; i++)
System.out.print((int)s.charAt(i)+"");
}
}


D:\myjava>java Test
GBK
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 10
7 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12
7 65533 20119 20750 21399 22130 22683 23229 23829 24385 25005 25666 26236 26841
27332 27924 28522 29018 12289 65509 12454 1046 9478 57449 57639 57829 36744 2881
8 20992 29359 36141 24739 39556 22391 35845 23194 29260 20392 22674 39042 33216
38386 23721 37321 32617 26837 20189 22318 33929 21721 24469 27781 24400 29623 27
547 33137 30477 38259 32817 31709 35978 40748 58091 58281 58471 65533
[/Quote]
good
ScAREcrOw_ss 2009-11-25
  • 打赏
  • 举报
回复

import java.net.*;
import java.io.*;
import java.nio.charset.Charset;

public class Test{
public static void main(String args[]) throws UnsupportedEncodingException{
System.out.println(Charset.defaultCharset().name());
byte[] bytes = new byte[256];
for(int i =0;i< 256;i++){
bytes[i] = (byte)i;
}
String s = new String(bytes);
for(int i =0, n= s.length();i<n ; i++)
System.out.print((int)s.charAt(i)+" ");
}
}




D:\myjava>java Test
GBK
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 10
7 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12
7 65533 20119 20750 21399 22130 22683 23229 23829 24385 25005 25666 26236 26841
27332 27924 28522 29018 12289 65509 12454 1046 9478 57449 57639 57829 36744 2881
8 20992 29359 36141 24739 39556 22391 35845 23194 29260 20392 22674 39042 33216
38386 23721 37321 32617 26837 20189 22318 33929 21721 24469 27781 24400 29623 27
547 33137 30477 38259 32817 31709 35978 40748 58091 58281 58471 65533

62,614

社区成员

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

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