C# JAVA字符串Unicode编码转换

 C#版本

        /// <summary>  
        /// 字符串转为UniCode码字符串  
        /// </summary>  
        /// <param name="s"></param>  
        /// <returns>字符串,不包含\u格式符号</returns>  
        public static string StringToUnicode(string s)
        {
            char[] charbuffers = s.ToCharArray();
            byte[] buffer;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < charbuffers.Length; i++)
            {
                buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString(CultureInfo.InvariantCulture));
                sb.Append(String.Format("{0:X2}{1:X2}", buffer[1], buffer[0]));
            }
            return sb.ToString();
        }
        /// <summary>  
        /// Unicode字符串转为正常字符串  
        /// </summary>  
        /// <param name="srcText">待转字符串,不要包含\u格式符号</param>  
        /// <returns></returns>  
        public static string UnicodeToString(string srcText)
        {
            string dst = "";
            string src = srcText;
            int len = srcText.Length / 4;
            for (int i = 0; i <= len - 1; i++)
            {
                string str = "";
                str = src.Substring(0, 4);
                src = src.Substring(4);
                byte[] bytes = new byte[2];
                bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString(CultureInfo.InvariantCulture));
                bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString(CultureInfo.InvariantCulture));
                dst += Encoding.Unicode.GetString(bytes);
            }
            return dst;
        }

 JAVA版本

    /**
     * 将字符串转成unicode
     * 
     * @param str
     *            待转字符串
     * @return unicode字符串,不包含\\u格式符号
     */
    public static String StringToUnicode(String str) {
        str = (str == null ? "" : str);
        String tmp;
        StringBuffer sb = new StringBuffer(1000);
        char c;
        int i, j;
        sb.setLength(0);
        for (i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            // sb.append("\\u");//不需要格式符合
            j = (c >>> 8); // 取出高8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
            j = (c & 0xFF); // 取出低8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);

        }
        return (new String(sb));
    }

    /**
     * 将unicode 字符串
     * 
     * @param str
     *            待转字符串,不要包含\\u格式符号
     * @return 普通字符串
     */
    public static String UnicodeToString(String str) {
        str = (str == null ? "" : str);
        int len = str.length()/4;
        StringBuffer sb = new StringBuffer(1000);

        for (int i = 0; i < len;i++) {
            String value = str.substring(0, 4);
            str = str.substring(4);
            int c = 0;
            for (int j = 0; j < value.length(); j++) {
                char tempChar = value.charAt(j);
                int t = 0;
                switch (tempChar) {
                case 'a':
                    t = 10;
                    break;
                case 'b':
                    t = 11;
                    break;
                case 'c':
                    t = 12;
                    break;
                case 'd':
                    t = 13;
                    break;
                case 'e':
                    t = 14;
                    break;
                case 'f':
                    t = 15;
                    break;
                default:
                    t = tempChar - 48;
                    break;
                }

                c += t * ((int) Math.pow(16, (value.length() - j - 1)));
            }
            sb.append((char) c);
        }
        return sb.toString();
    }

 

posted @ 2014-07-07 09:39  chenlh  阅读(1051)  评论(0编辑  收藏  举报