• java 编码转换


      

    package test;
    
    import java.util.Map;
    
    public class Test3
    {
        public static Map<String,Object> map;
        public static void main(String[] args) throws Exception
        {
            String str = "严";
    //        "GBK","UTF-8"
            byte[] bytes = str.getBytes("UTF-8");
            printHexString(bytes);
            System.out.println();
            String str2 = new String(bytes,"UTF-8");
            System.out.println(str2);
            
        }
        
        //将指定byte数组以16进制的形式打印到控制台   
        public static void printHexString( byte[] b) {     
           for (int i = 0; i < b.length; i++) {    
             String hex = Integer.toHexString(b[i] & 0xFF);    
             if (hex.length() == 1) {    
               hex = '0' + hex;    
             }    
             System.out.print(hex.toUpperCase() );    
           }    
          
        }  
    }

     相关函数

    byte[] bytes = str.getBytes("UTF-8");

     以指定的字符集对字符串进行编码,结果存放在byte数组中

    String str2 = new String(bytes,"UTF-8");

     以指定的字符集对给定的byte数组进行解码

    "严"的UTF编码为:E4B8A5,GBK编码为:D1CF

  • 相关阅读:
    设计模式之四 代理模式
    设计模式之四 建造者模式
    设计模式之三 模板模式
    设计模式之二 工厂模式
    如何使用Json-lib
    Java LoggingAPI 使用方法
    设计模式之一 单例模式
    Scrapy教程
    Scrapy简介
    Scrapy安装向导
  • 原文地址:https://www.cnblogs.com/hoonick/p/9933540.html
Copyright © 2020-2023  润新知