Skip to content

Instantly share code, notes, and snippets.

@XUJiahua
Created December 11, 2016 05:33
Show Gist options
  • Save XUJiahua/5fb2470619134d2840d9d4df7e93f02b to your computer and use it in GitHub Desktop.
Save XUJiahua/5fb2470619134d2840d9d4df7e93f02b to your computer and use it in GitHub Desktop.
String GBK转ASCII数据丢失,建议用byte2hex, hex2byte的形式
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) {
String myString = "中文123abc";
byte pText[] = myString.getBytes(Charset.forName("GBK")); // 10 bytes 2个字节代表1个中文
byte pText2[] = myString.getBytes(Charset.forName("UTF-8")); // 12 bytes 3个字节代表1个中文
byte pText3[] = myString.getBytes(Charset.forName("ASCII")); // 8 bytes 1个字节代表1个中文,精度损失了
String r1 = new String(pText, Charset.forName("GBK"));
String r2 = new String(pText, Charset.forName("ASCII")); // ����123abc GBK字节转ASCII造成数据丢失,转不回去了
byte r2Text[] = r2.getBytes(Charset.forName("ASCII")); // 与pText2比较,前4字节都不一样了
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment