zoukankan      html  css  js  c++  java
  • 编解码遇到的一些现象

    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)Javajdk1.8.0_201injava.exe" "-javaagent:C:Userszhaolei144AppDataLocalJetBrainsIntelliJ IDEA 2019.1.3libidea_rt.jar=59036:C:Userszhaolei144AppDataLocalJetBrainsIntelliJ IDEA 2019.1.3in" -Dfile.encoding=UTF-8 -classpath "C:Program Files (x86)Javajdk1.8.0_201jrelibcharsets.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibdeploy.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextaccess-bridge-32.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextcldrdata.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextdnsns.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextjaccess.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextjfxrt.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextlocaledata.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibext ashorn.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunec.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunjce_provider.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunmscapi.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunpkcs11.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextzipfs.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjavaws.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjce.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjfr.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjfxswt.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjsse.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibmanagement-agent.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibplugin.jar;C:Program Files (x86)Javajdk1.8.0_201jrelib esources.jar;C:Program Files (x86)Javajdk1.8.0_201jrelib t.jar;C:Userszhaolei144IdeaProjectsuntitledspringTest argetclasses;E:mavenlocalhostorgspringframeworkspring-context5.1.9.RELEASEspring-context-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-aop5.1.9.RELEASEspring-aop-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-beans5.1.9.RELEASEspring-beans-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-core5.1.9.RELEASEspring-core-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-jcl5.1.9.RELEASEspring-jcl-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-expression5.1.9.RELEASEspring-expression-5.1.9.RELEASE.jar;E:mavenlocalhostorgprojectlomboklombok1.16.10lombok-1.16.10.jar;E:mavenlocalhostcomjdcommonsso-uim-spring1.0.0-SNAPSHOTsso-uim-spring-1.0.0-20170317.010540-12.jar;E:mavenlocalhostorgslf4jslf4j-log4j121.6.4slf4j-log4j12-1.6.4.jar;E:mavenlocalhostorgslf4jslf4j-api1.6.4slf4j-api-1.6.4.jar;E:mavenlocalhostlog4jlog4j1.2.16log4j-1.2.16.jar;E:mavenlocalhostorgslf4jjcl-over-slf4j1.6.4jcl-over-slf4j-1.6.4.jar;E:mavenlocalhostorgspringframeworkspring-webmvc3.2.18.RELEASEspring-webmvc-3.2.18.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-web3.2.18.RELEASEspring-web-3.2.18.RELEASE.jar;E:mavenlocalhostcommons-langcommons-lang2.5commons-lang-2.5.jar;E:mavenlocalhostcomjdjimclijim-cli-jedis1.4.5-SNAPSHOTjim-cli-jedis-1.4.5-20190104.085644-17.jar;E:mavenlocalhostcomjdjimclijim-cli-driver1.4.5-SNAPSHOTjim-cli-driver-1.4.5-20190104.085556-13.jar;E:mavenlocalhostorgapachecommonscommons-lang33.3.2commons-lang3-3.3.2.jar;E:mavenlocalhostorgapachecommonscommons-collections44.1commons-collections4-4.1.jar;E:mavenlocalhostorgapachecommonscommons-pool22.4.1commons-pool2-2.4.1.jar;E:mavenlocalhostcomjdjimclijim-cli-api1.4.5-SNAPSHOTjim-cli-api-1.4.5-20190104.072227-48.jar;E:mavenlocalhostcomjdjimclijim-cli-config1.4.5-SNAPSHOTjim-cli-config-1.4.5-20190104.084315-38.jar;E:mavenlocalhostcomgooglecodefindbugsjsr3051.3.9jsr305-1.3.9.jar;E:mavenlocalhostcomjdcachecloudcachecloud-cfs-client.2.5-SNAPSHOTcachecloud-cfs-client-0.2.5-20160413.141000-5.jar;E:mavenlocalhostcomgoogleguavaguava19.0guava-19.0.jar;E:mavenlocalhostcomjdjst-core1.0.0-SNAPSHOTjst-core-1.0.0-20180123.113759-6.jar;E:mavenlocalhostcomjdjst-jvm-enhance1.0.0-SNAPSHOTjst-jvm-enhance-1.0.0-20180123.113918-19.jar;E:mavenlocalhostcomjdcommonjd-common-util1.1.2jd-common-util-1.1.2.jar;E:mavenlocalhostcommons-beanutilscommons-beanutils1.8.3commons-beanutils-1.8.3.jar;E:mavenlocalhostcommons-httpclientcommons-httpclient3.1commons-httpclient-3.1.jar;E:mavenlocalhostcommons-iocommons-io1.4commons-io-1.4.jar;E:mavenlocalhostcommons-loggingcommons-logging1.1.1commons-logging-1.1.1.jar;E:mavenlocalhostcommons-digestercommons-digester2.0commons-digester-2.0.jar;E:mavenlocalhostcommons-codeccommons-codec1.2commons-codec-1.2.jar;E:mavenlocalhostcommons-chaincommons-chain1.0commons-chain-1.0.jar;E:mavenlocalhostcommons-collectionscommons-collections3.2.1commons-collections-3.2.1.jar;E:mavenlocalhostcommons-dbcpcommons-dbcp1.4commons-dbcp-1.4.jar;E:mavenlocalhostcommons-poolcommons-pool1.5.4commons-pool-1.5.4.jar;E:mavenlocalhostcomalibabafastjson1.2.28fastjson-1.2.28.jar;E:mavenlocalhostorgapachehttpcomponentshttpclient4.5.2httpclient-4.5.2.jar;E:mavenlocalhostorgapachehttpcomponentshttpcore4.4.4httpcore-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)Javajdk1.8.0_201injava.exe" "-javaagent:C:Userszhaolei144AppDataLocalJetBrainsIntelliJ IDEA 2019.1.3libidea_rt.jar=59397:C:Userszhaolei144AppDataLocalJetBrainsIntelliJ IDEA 2019.1.3in" -Dfile.encoding=UTF-8 -classpath "C:Program Files (x86)Javajdk1.8.0_201jrelibcharsets.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibdeploy.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextaccess-bridge-32.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextcldrdata.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextdnsns.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextjaccess.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextjfxrt.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextlocaledata.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibext ashorn.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunec.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunjce_provider.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunmscapi.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextsunpkcs11.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibextzipfs.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjavaws.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjce.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjfr.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjfxswt.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibjsse.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibmanagement-agent.jar;C:Program Files (x86)Javajdk1.8.0_201jrelibplugin.jar;C:Program Files (x86)Javajdk1.8.0_201jrelib esources.jar;C:Program Files (x86)Javajdk1.8.0_201jrelib t.jar;C:Userszhaolei144IdeaProjectsuntitledspringTest argetclasses;E:mavenlocalhostorgspringframeworkspring-context5.1.9.RELEASEspring-context-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-aop5.1.9.RELEASEspring-aop-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-beans5.1.9.RELEASEspring-beans-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-core5.1.9.RELEASEspring-core-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-jcl5.1.9.RELEASEspring-jcl-5.1.9.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-expression5.1.9.RELEASEspring-expression-5.1.9.RELEASE.jar;E:mavenlocalhostorgprojectlomboklombok1.16.10lombok-1.16.10.jar;E:mavenlocalhostcomjdcommonsso-uim-spring1.0.0-SNAPSHOTsso-uim-spring-1.0.0-20170317.010540-12.jar;E:mavenlocalhostorgslf4jslf4j-log4j121.6.4slf4j-log4j12-1.6.4.jar;E:mavenlocalhostorgslf4jslf4j-api1.6.4slf4j-api-1.6.4.jar;E:mavenlocalhostlog4jlog4j1.2.16log4j-1.2.16.jar;E:mavenlocalhostorgslf4jjcl-over-slf4j1.6.4jcl-over-slf4j-1.6.4.jar;E:mavenlocalhostorgspringframeworkspring-webmvc3.2.18.RELEASEspring-webmvc-3.2.18.RELEASE.jar;E:mavenlocalhostorgspringframeworkspring-web3.2.18.RELEASEspring-web-3.2.18.RELEASE.jar;E:mavenlocalhostcommons-langcommons-lang2.5commons-lang-2.5.jar;E:mavenlocalhostcomjdjimclijim-cli-jedis1.4.5-SNAPSHOTjim-cli-jedis-1.4.5-20190104.085644-17.jar;E:mavenlocalhostcomjdjimclijim-cli-driver1.4.5-SNAPSHOTjim-cli-driver-1.4.5-20190104.085556-13.jar;E:mavenlocalhostorgapachecommonscommons-lang33.3.2commons-lang3-3.3.2.jar;E:mavenlocalhostorgapachecommonscommons-collections44.1commons-collections4-4.1.jar;E:mavenlocalhostorgapachecommonscommons-pool22.4.1commons-pool2-2.4.1.jar;E:mavenlocalhostcomjdjimclijim-cli-api1.4.5-SNAPSHOTjim-cli-api-1.4.5-20190104.072227-48.jar;E:mavenlocalhostcomjdjimclijim-cli-config1.4.5-SNAPSHOTjim-cli-config-1.4.5-20190104.084315-38.jar;E:mavenlocalhostcomgooglecodefindbugsjsr3051.3.9jsr305-1.3.9.jar;E:mavenlocalhostcomjdcachecloudcachecloud-cfs-client.2.5-SNAPSHOTcachecloud-cfs-client-0.2.5-20160413.141000-5.jar;E:mavenlocalhostcomgoogleguavaguava19.0guava-19.0.jar;E:mavenlocalhostcomjdjst-core1.0.0-SNAPSHOTjst-core-1.0.0-20180123.113759-6.jar;E:mavenlocalhostcomjdjst-jvm-enhance1.0.0-SNAPSHOTjst-jvm-enhance-1.0.0-20180123.113918-19.jar;E:mavenlocalhostcomjdcommonjd-common-util1.1.2jd-common-util-1.1.2.jar;E:mavenlocalhostcommons-beanutilscommons-beanutils1.8.3commons-beanutils-1.8.3.jar;E:mavenlocalhostcommons-httpclientcommons-httpclient3.1commons-httpclient-3.1.jar;E:mavenlocalhostcommons-iocommons-io1.4commons-io-1.4.jar;E:mavenlocalhostcommons-loggingcommons-logging1.1.1commons-logging-1.1.1.jar;E:mavenlocalhostcommons-digestercommons-digester2.0commons-digester-2.0.jar;E:mavenlocalhostcommons-codeccommons-codec1.2commons-codec-1.2.jar;E:mavenlocalhostcommons-chaincommons-chain1.0commons-chain-1.0.jar;E:mavenlocalhostcommons-collectionscommons-collections3.2.1commons-collections-3.2.1.jar;E:mavenlocalhostcommons-dbcpcommons-dbcp1.4commons-dbcp-1.4.jar;E:mavenlocalhostcommons-poolcommons-pool1.5.4commons-pool-1.5.4.jar;E:mavenlocalhostcomalibabafastjson1.2.28fastjson-1.2.28.jar;E:mavenlocalhostorgapachehttpcomponentshttpclient4.5.2httpclient-4.5.2.jar;E:mavenlocalhostorgapachehttpcomponentshttpcore4.4.4httpcore-4.4.4.jar" com.zhaolei.proxyTest.Test
    * 璧电�
    * 赵磊
    */

    }
    byte[]数组于String的换转:当byte数组中的值为负数时经过解码再编码得不到原来的值
    心有多大,天有多高,一起奋斗!!
  • 相关阅读:
    关于给Tomcat设置maxPostSize的问题
    完美激活PyCharm教程
    Tomcat +Nginx+Redis实现session共享
    python 上传多文件
    吸引注意力的动画
    Create React App 安装less 报错
    2020软件工程作业01
    yum用法
    rpm包的管理
    共享依赖库的误移或误删解决办法
  • 原文地址:https://www.cnblogs.com/zhaolei1996/p/11796780.html
Copyright © 2011-2022 走看看