import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestCharset {
public static void main(String[] args) throws IOException {
try {
new TestCharset().execute();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void execute() throws IOException {
File f=new File("c:\\t2.txt");
BufferedReader br=new BufferedReader(new FileReader(f));
String s=br.readLine();
while(s!=null){
byte[] bytesISO8859 = null;
byte[] bytesGBK = null;
try {
// bytesISO8859 = s.getBytes("iso-8859-1");
bytesGBK = s.getBytes("UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
// System.out.println("-------------- \n 8859 bytes:");
// System.out.println("bytes is: " + arrayToString(bytesISO8859));
// System.out.println("hex format is:" + encodeHex(bytesISO8859));
// System.out.println();
// System.out.println("-------------- \n UTF-8 bytes:");
// System.out.println("bytes is: " + arrayToString(bytesGBK));
// System.out.println("hex format is:" + encodeHex(bytesGBK));
System.out.println(encodeHex(bytesGBK));
s=br.readLine();
}
}
public static final String encodeHex(byte[] bytes) {
StringBuffer buff = new StringBuffer(bytes.length * 2);
String b;
for (int i = 0; i < bytes.length; i++) {
b = Integer.toHexString(bytes[i]);
// byte是两个字节的,
// 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append("%");
buff.append(b.length() > 2 ? b.substring(6, 8) : b);
}
return buff.toString();
}
public static final String arrayToString(byte[] bytes) {
StringBuffer buff = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
buff.append(bytes[i] + " ");
}
return buff.toString();
}
}
运行一下就明白了..
3325

被折叠的 条评论
为什么被折叠?



