ITPub博客

首页 > IT职业 > IT生活 > unicode转码工具类

unicode转码工具类

IT生活 作者:gamebus 时间:2021-09-09 09:26:28 0 删除 编辑
	public static String decodeUnicode(String str) {		Charset set = Charset.forName("UTF-16");		Pattern p = Pattern.compile("\\u([0-9a-fA-F]{4})");		Matcher m = p.matcher(str);		int start = 0;		int start2 = 0;		StringBuffer sb = new StringBuffer();		while (m.find(start)) {			start2 = m.start();			if (start2 > start) {				String seg = str.substring(start, start2);				sb.append(seg);			}			String code = m.group(1);			int i = Integer.valueOf(code, 16);			byte[] bb = new byte[4];			bb[0] = (byte) ((i >> 8) & 0xFF);			bb[1] = (byte) (i & 0xFF);			ByteBuffer b = ByteBuffer.wrap(bb);			sb.append(String.valueOf(set.decode(b)).trim());			start = m.end();		}		start2 = str.length();		if (start2 > start) {			String seg = str.substring(start, start2);			sb.append(seg);		}		return sb.toString();	}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4687/viewspace-2803029/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    155
  • 访问量
    13194