zoukankan      html  css  js  c++  java
  • Java中16进制与字符串之间的相互转换

    笔者前几日在开服过程中需要将字符串转化成为16进制的字符串,在网上找到了一些方法尝试之后,均发现存在一个问题-->字符串转为16进制后再转回来,英文正常,中文出现乱码

    经过考虑决定通过以下方式进行解决: 

      1)在将字符串转为16进制之前先进行一次转化,先将其转化成为Unicode编码(相当于把中文用英文字符代替),在转化成为16进制

      2)相反的,在十六进制转换为字符串后的得到的是Unicode编码,此时再将Unicode编码解码即可获取原始字符串

    代码如下:

      *字符串转化为Unicode编码:

    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {
      StringBuffer unicode = new StringBuffer();
      for (int i = 0; i < string.length(); i++) {
        // 取出每一个字符
        char c = string.charAt(i);
        // 转换为unicode
        unicode.append("\u" + Integer.toHexString(c));
      }
      return unicode.toString();
    }  

    *字符串转为16进制

    /**
     * 字符串转化成为16进制字符串
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    *16进制转为字符串

    /**
     * 16进制转换成为string类型字符串
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    *Unicode转为字符串

    /**
     * unicode 转字符串
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }

    此方法虽然解决了转化过程中中文乱码的问题,但是过于复杂,笔者后来又发现一种新的转化方式,可直接转化,中文不乱码,代码如下:

      *字符串转16进制

     1 /**
     2  * 字符串转换成为16进制(无需Unicode编码)
     3  * @param str
     4  * @return
     5  */
     6 public static String str2HexStr(String str) {
     7     char[] chars = "0123456789ABCDEF".toCharArray();
     8     StringBuilder sb = new StringBuilder("");
     9     byte[] bs = str.getBytes();
    10     int bit;
    11     for (int i = 0; i < bs.length; i++) {
    12         bit = (bs[i] & 0x0f0) >> 4;
    13         sb.append(chars[bit]);
    14         bit = bs[i] & 0x0f;
    15         sb.append(chars[bit]);
    16         // sb.append(' ');
    17     }
    18     return sb.toString().trim();
    19 }

    *16进制转为字符串

     1 /**
     2  * 16进制直接转换成为字符串(无需Unicode解码)
     3  * @param hexStr
     4  * @return
     5  */
     6 public static String hexStr2Str(String hexStr) {
     7     String str = "0123456789ABCDEF";
     8     char[] hexs = hexStr.toCharArray();
     9     byte[] bytes = new byte[hexStr.length() / 2];
    10     int n;
    11     for (int i = 0; i < bytes.length; i++) {
    12         n = str.indexOf(hexs[2 * i]) * 16;
    13         n += str.indexOf(hexs[2 * i + 1]);
    14         bytes[i] = (byte) (n & 0xff);
    15     }
    16     return new String(bytes);
    17 }
  • 相关阅读:
    D. Babaei and Birthday Cake--- Codeforces Round #343 (Div. 2)
    Vijos P1389婚礼上的小杉
    AIM Tech Round (Div. 2) C. Graph and String
    HDU 5627Clarke and MST
    bzoj 3332 旧试题
    codeforces 842C Ilya And The Tree
    codesforces 671D Roads in Yusland
    Travelling
    codeforces 606C Sorting Railway Cars
    codeforces 651C Watchmen
  • 原文地址:https://www.cnblogs.com/kliine/p/9987512.html
Copyright © 2011-2022 走看看