posts - 495,comments - 227,trackbacks - 0

 

public static String unicodeEncode(String strText) {
        
char c;
        String strRet 
= "";
        
int intAsc;
        String strHex;
        
for (int i = 0; i < strText.length(); i++{
            c 
= strText.charAt(i);
            intAsc 
= c;
            
if (intAsc > 128{
                strHex 
= Integer.toHexString(intAsc);
                strRet 
+= "\\u" + strHex;
            }
 else {
                strRet 
= strRet + c;
            }

        }

        
return strRet;
    }

    
    
public static String unicodeDecode(String strText) {
        StringBuilder sb 
= new StringBuilder();
        
int i = 0;
        
char c;
        
while (i < strText.length()) {
            c 
= strText.charAt(i);
            
if (c == '\\' && (i + 1!= strText.length() && strText.charAt(i + 1== 'u'{
                sb.append((
char) Integer.parseInt(strText.substring(i + 2, i + 6), 16));
                i 
+= 6;
            }
 else {
                sb.append(c);
                i
++;
            }

        }

        
return sb.toString();
    }
posted on 2011-04-22 13:53 SIMONE 阅读(1230) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: