java爬取网页Unicode 转 utf-8 汉字

    xiaoxiao2024-11-25  20

    废话少说直接上代码

    package util; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author stone *<p>Unicode 转utf-8 * */ 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")); } }

    转载请注明原文地址: https://ju.6miu.com/read-1294007.html
    最新回复(0)