unicode java转换_JAVA转化Unicode编码

package yyl.example.basic.codec;

import java.util.Locale;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class UnicodeUtil {

//编码

public static String encode(String string) {

StringBuilder sbr = new StringBuilder(string.length() * 4);

for (char ch : string.toCharArray()) {

if (ch > 0xfff) {

sbr.append("\\u");

sbr.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));

} else if (ch > 0xff) {

sbr.append("\\u0");

sbr.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));

} else if (ch > 0x7f) {

sbr.append("\\u00");

sbr.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));

} else if (ch < 32) {

switch (ch) {

case '\b':

sbr.append('\\');

sbr.append('b');

break;

case '\n':

sbr.append('\\');

sbr.append('n');

break;

case '\t':

sbr.append('\\');

sbr.append('t');

break;

case '\f':

sbr.append('\\');

sbr.append('f');

break;

case '\r':

sbr.append('\\');

sbr.append('r');

break;

default:

if (ch > 0xf) {

sbr.append("\\u00");

sbr.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));

} else {

sbr.append("\\u000");

sbr.append(Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));

}

break;

}

} else {

switch (ch) {

case '\'':

sbr.append("\\u0027");

break;

case '"':

case '\\':

sbr.append("\\");

sbr.append(ch);

break;

default:

sbr.append(ch);

break;

}

}

}

return sbr.toString();

}

//解码

public static String decode(String unicode) {

char[] chars = unicode.toCharArray();

StringBuilder buffer = new StringBuilder();

for (int i = 0, len = chars.length; i < len;) {

char c = chars[i++];

switch (c) {

case 0:

case '\n':

case '\r':

break;

case '\\':

c = chars[i++];

switch (c) {

case 'b':

buffer.append('\b');

break;

case 't':

buffer.append('\t');

break;

case 'n':

buffer.append('\n');

break;

case 'f':

buffer.append('\f');

break;

case 'r':

buffer.append('\r');

break;

case 'u':

buffer.append((char) Integer.parseInt(new String(new char[] {//

chars[i++], chars[i++], chars[i++], chars[i++] //

}), 16));//

break;

case 'x':

buffer.append((char) Integer.parseInt(new String(new char[] {//

chars[i++], chars[i++] //

}), 16));//

break;

default:

buffer.append(c);

}

break;

default:

buffer.append(c);

}

}

return buffer.toString();

}

/**

* 方法2

* @param unicodeStr

* @return

*/

public static String unicodeToString(String unicodeStr) {

Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");

Matcher matcher = pattern.matcher(unicodeStr);

char ch;

while (matcher.find()) {

ch = (char) Integer.parseInt(matcher.group(2), 16);

unicodeStr = unicodeStr.replace(matcher.group(1), ch + "");

}

return unicodeStr;

}

public static void main(String[] args) {

String unicode = encode("你好Unicode");

System.out.println(unicode);

StringBuilder sbr = new StringBuilder();

for (int i = 0; i < 1000; i++) {

sbr.append(unicode);

}

unicode = sbr.toString();

//System.out.println(decode(unicode));

long l = System.currentTimeMillis();

for (int i = 0; i < 10000; i++) {

decode(unicode);

//unicodeToString(unicode);

}

System.out.println(System.currentTimeMillis() - l);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值