java Unicode 转 utf-8 汉字

时间:2022-04-11 20:16:08
// 从网络上下载到的网页经常是Unicode格式的,这个工具类可以将<span style="font-family: Arial, Helvetica, sans-serif;">Unicode格式转换为utf-8格式,也就是讲\u2422\u3243之类的编码转换为汉字,非常好用,双引号什么的没有影响。</span>
package util;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tian
 *
 */
public class CharSetUtil {

	/**
	 * 解码 Unicode \\uXXXX
	 * @param str
	 * @return
	 */
	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() ;
	}
	
	public static void main(String[] args) {
		System.out.println( decodeUnicode("\\u5907abbbbbs\\u5907"));
	}
}