unicode的编码解码


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


    /**
     * unicode编码
     * @param String str
     * @return String
     */
    public static String encodeUnicode(String string) {
         
        StringBuffer unicode = new StringBuffer();
     
        for (int i = 0; i < string.length(); i++) {
     
            // 取出每一个字符
            char c = string.charAt(i);
     
            // 转换为unicode
            unicode.append("\\u00" + Integer.toHexString(c));
        }
     
        return unicode.toString();
    }

    /**
     * unicode 解码
     * @param String str
     * @return String
     */
     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() ;
    }

爬去网页时获取到的时unicode编码过的字符串,用该方法解码即可
需要注意的是  如果需要转码解码的字符串中含有中文,则这两种方法是不适用的。
--------------------- 
作者:NumbIssac 
来源:CSDN 
原文:https://blog.csdn.net/weixin_38453685/article/details/77990645 
版权声明:本文为博主原创文章,转载请附上博文链接!

### 回答1: UNICODE 编码解码可以通过 PHP 内置的函数 mb_convert_encoding 和 iconv 来实现。 编码: ``` $str = '你好'; $encoded = mb_convert_encoding($str, 'UTF-16', 'UTF-8'); ``` 解码: ``` $decoded = mb_convert_encoding($encoded, 'UTF-8', 'UTF-16'); ``` 或者: 编码: ``` $str = '你好'; $encoded = iconv('UTF-8', 'UTF-16', $str); ``` 解码: ``` $decoded = iconv('UTF-16', 'UTF-8', $encoded); ``` ### 回答2: 在PHP中,可以使用内置的函数来实现UNICODE编码解码。 对于编码: 1. 首先,我们需要将字符串转换为UTF-8编码,以确保包含任何特殊字符。 2. 然后,使用mb_convert_encoding函数将UTF-8编码的字符串转换为Unicode。 3. 最后,使用bin2hex函数将Unicode编码的字符串转换为Unicode编码的16进制表示。 下面是一个示例代码片段,用于在PHP中实现UNICODE编码: ```php $str = "你好,世界!"; // 要编码的字符串 // 转换为UTF-8编码 $str = mb_convert_encoding($str, 'UTF-8'); // 将UTF-8编码的字符串转换为Unicode $str_unicode = mb_convert_encoding($str, 'UCS-2LE', 'UTF-8'); // 将Unicode编码的字符串转换为Unicode编码的16进制表示 $str_unicode_hex = bin2hex($str_unicode); echo $str_unicode_hex; // 输出编码后的字符串 ``` 对于解码: 1. 首先,使用hex2bin函数将Unicode编码的16进制表示转换为Unicode编码的字符串。 2. 然后,使用mb_convert_encoding函数将Unicode编码的字符串转换为UTF-8编码的字符串。 下面是一个示例代码片段,用于在PHP中实现UNICODE解码: ```php $str_unicode_hex = "4f60597d65252c4e世界21"; // 要解码Unicode编码的16进制表示 // 将Unicode编码的16进制表示转换为Unicode编码的字符串 $str_unicode = hex2bin($str_unicode_hex); // 将Unicode编码的字符串转换为UTF-8编码的字符串 $str = mb_convert_encoding($str_unicode, 'UTF-8', 'UCS-2LE'); echo $str; // 输出解码后的字符串 ``` 通过以上代码示例,可以使用PHP实现UNICODE编码解码。 ### 回答3: 使用PHP实现UNICODE编码解码可以通过以下步骤实现: 1. 编码:将字符串转换为UNICODE编码。 可以使用PHP内置的函数 `mb_convert_encoding` 来实现。使用这个函数,你可以将字符串从指定的字符集转换为UNICODE编码。例如,将一个UTF-8编码的字符串转换为UNICODE编码,可以使用以下代码: ```php $str = '你好'; $unicodeStr = mb_convert_encoding($str, 'unicode', 'utf-8'); echo $unicodeStr; ``` 这段代码将输出:`\u4f60\u597d`,其中`\u4f60`表示字符"你"的UNICODE编码,`\u597d`表示字符"好"的UNICODE编码。 2. 解码:将UNICODE编码转换为字符串。 可以使用PHP内置的函数 `json_decode` 来将UNICODE编码转换为字符串。 `json_decode` 函数默认会将UNICODE编码的字符串转换为UTF-8编码的字符串。例如,将一个UNICODE编码的字符串`\u4f60\u597d`转换为UTF-8编码的字符串,可以使用以下代码: ```php $str = '\u4f60\u597d'; $decodedStr = json_decode('"' . $str . '"'); echo $decodedStr; ``` 这段代码将输出:"你好"。 这就是使用PHP实现UNICODE编码解码的基本步骤。你可以根据需要对其进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值