src/util/Gb2312ToUtf.java
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
package util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
/**
* 2007-8-10 jyin at gomez dot com
*/
public class Gb2312ToUtf {
public static void main(String[] args) {
String str = "This is a test for *中网!@#$。,?";
try {
File f = new File("D:/test.txt");
FileOutputStream fio = new FileOutputStream(f);
String s = gbToUtf8(str);
fio.write(s.getBytes("UTF-8"));
fio.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String gbToUtf8(String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
String s = str.substring(i, i + 1);
if (s.charAt(0) > 0x80) {
byte[] bytes = s.getBytes("Unicode");
String binaryStr = "";
for (int j = 2; j < bytes.length; j += 2) {
// the first byte
String hexStr = getHexString(bytes[j + 1]);
String binStr = getBinaryString(Integer.valueOf(hexStr, 16));
binaryStr += binStr;
// the second byte
hexStr = getHexString(bytes[j]);
binStr = getBinaryString(Integer.valueOf(hexStr, 16));
binaryStr += binStr;
}
// convert unicode to utf-8
String s1 = "1110" + binaryStr.substring(0, 4);
String s2 = "10" + binaryStr.substring(4, 10);
String s3 = "10" + binaryStr.substring(10, 16);
byte[] bs = new byte[3];
bs[0] = Integer.valueOf(s1, 2).byteValue();
bs[1] = Integer.valueOf(s2, 2).byteValue();
bs[2] = Integer.valueOf(s3, 2).byteValue();
String ss = new String(bs, "UTF-8");
sb.append(ss);
} else {
sb.append(s);
}
}
return sb.toString();
}
private static String getHexString(byte b) {
后面还有 19 行代码,购买后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。