• unicode正反编码


     1 /**
     2      * 将String转换为Unicode编码的字符串
     3      * Unicode编码规则:Unicode码对每一个字符用4位16进制数表示。
     4      * 具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,
     5      * 如果转化的16进制数的长度不足2位,则在高位补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\\u" 即可。
     6      * 
     7      * @param str
     8      * @return
     9      */
    10     public static String convertStrToUnicode(String str) {
    11         if (str == null || str.length() == 0) {
    12             return "";
    13         }
    14         StringBuffer sb = new StringBuffer(1000);
    15         // 临时变量
    16         int hexNumber;
    17         char c;
    18         String hexStr;
    19         for (int i = 0; i < str.length(); i++) {
    20             // 加上\\u前缀
    21             sb.append("\\u");
    22             // 取出第i为的char字符
    23             c = str.charAt(i);
    24             // 取出高8位
    25             hexNumber = (c >>> 8);
    26             hexStr = Integer.toHexString(hexNumber);
    27             if (hexStr.length() == 1)
    28                 sb.append("0");
    29             sb.append(hexStr);
    30 
    31             // 取出低8位
    32             hexNumber = (c & 0xFF);
    33             hexStr = Integer.toHexString(hexNumber);
    34             if (hexStr.length() == 1)
    35                 sb.append("0");
    36             sb.append(hexStr);
    37         }
    38         return sb.toString();
    39     }
    40 
    41     /**
    42      * 将Unicode编码的字符串转换为String
    43      * 
    44      * @param unicodeStr
    45      * @return
    46      */
    47     public static String convertUnicodeToStr(String unicodeStr) {
    48         if (unicodeStr == null || unicodeStr.length() == 0) {
    49             return "";
    50         }
    51         String unicodePrefix = "\\u";
    52         String tempStr;
    53         String tempHexStr;
    54         StringBuffer sb = new StringBuffer(1000);
    55         while (unicodeStr.contains(unicodePrefix)) {
    56             // 获取第一次出现\\u的index
    57             int firstIndex = unicodeStr.indexOf(unicodePrefix);
    58             // 获取第二次出现\\u的index
    59             int secondIndex = unicodeStr.indexOf(unicodePrefix, firstIndex + 2);
    60             // 将第一出现与第二次出现中间的部分,截取下来
    61             if (secondIndex == -1) {
    62                 tempStr = unicodeStr.substring(firstIndex);
    63             } else {
    64                 tempStr = unicodeStr.substring(firstIndex, secondIndex);
    65             }
    66             tempHexStr = tempStr.substring(tempStr.indexOf(unicodePrefix) + 2);
    67             if (tempHexStr.length() == 4) {
    68                 sb.append((char) Integer.parseInt(tempHexStr, 16));
    69             }
    70             // 将第二次出现以后的部分截取下来
    71             if (secondIndex == -1) {
    72                 unicodeStr = "";
    73             } else {
    74                 unicodeStr = unicodeStr.substring(secondIndex);
    75             }
    76         }
    77         return sb.toString();
    78     }
  • 相关阅读:
    【PAT Advanced Level】1008. Elevator (20)
    模块的耦合和内聚
    《深入理解计算机系统》--链接
    HDU 1077Catching Fish(简单计算几何)
    [置顶] VC++界面编程之--自定义CEdit(编辑框)皮肤
    使用VS2012 开发SharePoint 2013 声明式的action(activity) 综合实例
    java 创建线程的三种方法Callable,Runnable,Thread比较及用法
    代码审计技巧讲解
    IP地址后面斜杠加具体数字详解
    80端口被system进程占用解决方法
  • 原文地址:https://www.cnblogs.com/xmb7/p/3071216.html
Copyright © 2020-2023  润新知