把asc码的汉字转为utf-8和iso8859-1


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();
}
}




运行一下就明白了..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值