zoukankan      html  css  js  c++  java
  • 普通字符串与Hex编码字符串之间转换

    import java.io.UnsupportedEncodingException;
    import org.apache.commons.codec.binary.Hex;
    
    public class Example {
        /**
         * 将普通字符串转换成Hex编码字符串
         * 
         * @param dataCoding 编码格式,15表示GBK编码,8表示UnicodeBigUnmarked编码,0表示ISO8859-1编码
         * @param realStr 普通字符串
         * @return Hex编码字符串
         * @throws UnsupportedEncodingException 
         */
        public static String encodeHexStr(int dataCoding, String realStr) {
            String hexStr = null;
            if (realStr != null) {
                try {
                    if (dataCoding == 15) {
                        hexStr = new String(Hex.encodeHex(realStr.getBytes("GBK")));
                    } else if ((dataCoding & 0x0C) == 0x08) {
                        hexStr = new String(Hex.encodeHex(realStr.getBytes("UnicodeBigUnmarked")));
                    } else {
                        hexStr = new String(Hex.encodeHex(realStr.getBytes("ISO8859-1")));
                    }
                } catch (UnsupportedEncodingException e) {
                    System.out.println(e.toString());
                }
            }
            return hexStr;
        }
        
        /**
         * 将Hex编码字符串转换成普通字符串
         * 
         * @param dataCoding 反编码格式,15表示GBK编码,8表示UnicodeBigUnmarked编码,0表示ISO8859-1编码
         * @param hexStr Hex编码字符串
         * @return 普通字符串
         */
        public static String decodeHexStr(int dataCoding, String hexStr) {
            String realStr = null;
            try {
                if (hexStr != null) {
                    if (dataCoding == 15) {
                        realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "GBK");
                    } else if ((dataCoding & 0x0C) == 0x08) {
                        realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "UnicodeBigUnmarked");
                    } else {
                        realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "ISO8859-1");
                    }
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            
            return realStr;
        }
    
    }
  • 相关阅读:
    vue使用talkIngData统计
    vue项目中使用百度统计
    vue的指令修饰符
    提问:
    整理心情再投入下一个阶段
    CSS写三角形
    单行文本和多行文本超出隐藏
    清除浮动的方法
    用JS表示斐波拉契数列
    vue中使用动态挂载和懒加载,实现点击导航栏菜单弹出不同弹框
  • 原文地址:https://www.cnblogs.com/mcahkf/p/5177376.html
Copyright © 2011-2022 走看看