utf-8 与 gbk 的编解码问题:

用gbk编码后用utf-8解码,再用逆过程还原后得不到正确得结果(因为gbk编码后再用utf-8解码会造成数据得丢失),这个现象不针对于英文字符,英文字符可以正确得出结果,因为英文字符在各个编码表中是兼容的

public static void main(String[] args) throws UnsupportedEncodingException {
/* ProxyClass proxyClass = new ProxyClass(new UserImpl());
User user = (User) Proxy.newProxyInstance(User.class.getClassLoader(),new Class[]{User.class},proxyClass);
user.say();*/
String name = "赵磊";
/**
* GBK编码
* utf-8解码
*/
byte[] b = name.getBytes("GBK");
name = new String(b,"UTF-8");
System.out.println(name);//肯定不一致,因为编码与解码方式不同
/**
* 用逆过程还原
* 1.用utf-8编码
* 2.用GBK解码
*/
b = name.getBytes("UTF-8");
name = new String (b,"GBK");
System.out.println(name);//因为用gbk编码再用UTF-8解码会造成数据的丢失,所以逆过程也不能还原
/**
* 结果:
* "C:\Program Files (x86)\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Users\zhaolei144\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=59036:C:\Users\zhaolei144\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\zhaolei144\IdeaProjects\untitled\springTest\target\classes;E:\mavenlocalhost\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\projectlombok\lombok\1.16.10\lombok-1.16.10.jar;E:\mavenlocalhost\com\jd\common\sso-uim-spring\1.0.0-SNAPSHOT\sso-uim-spring-1.0.0-20170317.010540-12.jar;E:\mavenlocalhost\org\slf4j\slf4j-log4j12\1.6.4\slf4j-log4j12-1.6.4.jar;E:\mavenlocalhost\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar;E:\mavenlocalhost\log4j\log4j\1.2.16\log4j-1.2.16.jar;E:\mavenlocalhost\org\slf4j\jcl-over-slf4j\1.6.4\jcl-over-slf4j-1.6.4.jar;E:\mavenlocalhost\org\springframework\spring-webmvc\3.2.18.RELEASE\spring-webmvc-3.2.18.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-web\3.2.18.RELEASE\spring-web-3.2.18.RELEASE.jar;E:\mavenlocalhost\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-jedis\1.4.5-SNAPSHOT\jim-cli-jedis-1.4.5-20190104.085644-17.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-driver\1.4.5-SNAPSHOT\jim-cli-driver-1.4.5-20190104.085556-13.jar;E:\mavenlocalhost\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;E:\mavenlocalhost\org\apache\commons\commons-collections4\4.1\commons-collections4-4.1.jar;E:\mavenlocalhost\org\apache\commons\commons-pool2\2.4.1\commons-pool2-2.4.1.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-api\1.4.5-SNAPSHOT\jim-cli-api-1.4.5-20190104.072227-48.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-config\1.4.5-SNAPSHOT\jim-cli-config-1.4.5-20190104.084315-38.jar;E:\mavenlocalhost\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;E:\mavenlocalhost\com\jd\cachecloud\cachecloud-cfs-client\0.2.5-SNAPSHOT\cachecloud-cfs-client-0.2.5-20160413.141000-5.jar;E:\mavenlocalhost\com\google\guava\guava\19.0\guava-19.0.jar;E:\mavenlocalhost\com\jd\jst-core\1.0.0-SNAPSHOT\jst-core-1.0.0-20180123.113759-6.jar;E:\mavenlocalhost\com\jd\jst-jvm-enhance\1.0.0-SNAPSHOT\jst-jvm-enhance-1.0.0-20180123.113918-19.jar;E:\mavenlocalhost\com\jd\common\jd-common-util\1.1.2\jd-common-util-1.1.2.jar;E:\mavenlocalhost\commons-beanutils\commons-beanutils\1.8.3\commons-beanutils-1.8.3.jar;E:\mavenlocalhost\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;E:\mavenlocalhost\commons-io\commons-io\1.4\commons-io-1.4.jar;E:\mavenlocalhost\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\mavenlocalhost\commons-digester\commons-digester\2.0\commons-digester-2.0.jar;E:\mavenlocalhost\commons-codec\commons-codec\1.2\commons-codec-1.2.jar;E:\mavenlocalhost\commons-chain\commons-chain\1.0\commons-chain-1.0.jar;E:\mavenlocalhost\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\mavenlocalhost\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;E:\mavenlocalhost\commons-pool\commons-pool\1.5.4\commons-pool-1.5.4.jar;E:\mavenlocalhost\com\alibaba\fastjson\1.2.28\fastjson-1.2.28.jar;E:\mavenlocalhost\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\mavenlocalhost\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar" com.zhaolei.proxyTest.Test
* ����
* 锟斤拷锟斤拷
*/
}
用utf-8编码再用gbk解码 ,再逆过程后可以得到正确的结果:
 public static void main(String[] args) throws UnsupportedEncodingException {
/* ProxyClass proxyClass = new ProxyClass(new UserImpl());
User user = (User) Proxy.newProxyInstance(User.class.getClassLoader(),new Class[]{User.class},proxyClass);
user.say();*/
String name = "赵磊";
/**
* utf-8编码
* GBK解码
*/
byte[] b = name.getBytes("UTF-8");
name = new String(b,"GBK");
System.out.println(name);//肯定不一致,因为编码与解码方式不同
/**
* 用逆过程还原
* 1.用GBK编码
* 2.用UTF-8解码
*/
b = name.getBytes("GBK");
name = new String (b,"UTF-8");
System.out.println(name);//因为用gbk编码再用UTF-8解码会造成数据的丢失,所以逆过程也不能还原
}
/**
* 结果:
* "C:\Program Files (x86)\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Users\zhaolei144\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=59397:C:\Users\zhaolei144\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\zhaolei144\IdeaProjects\untitled\springTest\target\classes;E:\mavenlocalhost\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;E:\mavenlocalhost\org\projectlombok\lombok\1.16.10\lombok-1.16.10.jar;E:\mavenlocalhost\com\jd\common\sso-uim-spring\1.0.0-SNAPSHOT\sso-uim-spring-1.0.0-20170317.010540-12.jar;E:\mavenlocalhost\org\slf4j\slf4j-log4j12\1.6.4\slf4j-log4j12-1.6.4.jar;E:\mavenlocalhost\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar;E:\mavenlocalhost\log4j\log4j\1.2.16\log4j-1.2.16.jar;E:\mavenlocalhost\org\slf4j\jcl-over-slf4j\1.6.4\jcl-over-slf4j-1.6.4.jar;E:\mavenlocalhost\org\springframework\spring-webmvc\3.2.18.RELEASE\spring-webmvc-3.2.18.RELEASE.jar;E:\mavenlocalhost\org\springframework\spring-web\3.2.18.RELEASE\spring-web-3.2.18.RELEASE.jar;E:\mavenlocalhost\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-jedis\1.4.5-SNAPSHOT\jim-cli-jedis-1.4.5-20190104.085644-17.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-driver\1.4.5-SNAPSHOT\jim-cli-driver-1.4.5-20190104.085556-13.jar;E:\mavenlocalhost\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;E:\mavenlocalhost\org\apache\commons\commons-collections4\4.1\commons-collections4-4.1.jar;E:\mavenlocalhost\org\apache\commons\commons-pool2\2.4.1\commons-pool2-2.4.1.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-api\1.4.5-SNAPSHOT\jim-cli-api-1.4.5-20190104.072227-48.jar;E:\mavenlocalhost\com\jd\jim\cli\jim-cli-config\1.4.5-SNAPSHOT\jim-cli-config-1.4.5-20190104.084315-38.jar;E:\mavenlocalhost\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;E:\mavenlocalhost\com\jd\cachecloud\cachecloud-cfs-client\0.2.5-SNAPSHOT\cachecloud-cfs-client-0.2.5-20160413.141000-5.jar;E:\mavenlocalhost\com\google\guava\guava\19.0\guava-19.0.jar;E:\mavenlocalhost\com\jd\jst-core\1.0.0-SNAPSHOT\jst-core-1.0.0-20180123.113759-6.jar;E:\mavenlocalhost\com\jd\jst-jvm-enhance\1.0.0-SNAPSHOT\jst-jvm-enhance-1.0.0-20180123.113918-19.jar;E:\mavenlocalhost\com\jd\common\jd-common-util\1.1.2\jd-common-util-1.1.2.jar;E:\mavenlocalhost\commons-beanutils\commons-beanutils\1.8.3\commons-beanutils-1.8.3.jar;E:\mavenlocalhost\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;E:\mavenlocalhost\commons-io\commons-io\1.4\commons-io-1.4.jar;E:\mavenlocalhost\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\mavenlocalhost\commons-digester\commons-digester\2.0\commons-digester-2.0.jar;E:\mavenlocalhost\commons-codec\commons-codec\1.2\commons-codec-1.2.jar;E:\mavenlocalhost\commons-chain\commons-chain\1.0\commons-chain-1.0.jar;E:\mavenlocalhost\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\mavenlocalhost\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;E:\mavenlocalhost\commons-pool\commons-pool\1.5.4\commons-pool-1.5.4.jar;E:\mavenlocalhost\com\alibaba\fastjson\1.2.28\fastjson-1.2.28.jar;E:\mavenlocalhost\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\mavenlocalhost\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar" com.zhaolei.proxyTest.Test
* 璧电
* 赵磊
*/

}
byte[]数组于String的换转:当byte数组中的值为负数时经过解码再编码得不到原来的值
12-30 18:55