惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Schneier on Security
Engineering at Meta
Engineering at Meta
I
InfoQ
L
LangChain Blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
WordPress大学
WordPress大学
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Last Watchdog
The Last Watchdog
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
爱范儿
爱范儿
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
AI
AI
T
Tor Project blog
I
Intezer
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
N
News and Events Feed by Topic
Latest news
Latest news
S
Security Affairs
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
S
Securelist

博客园 - guoadou

新的开始 《项目管理艺术》读书笔记 —— 决定做什么的常见不良决策一览 《项目管理艺术》读书笔记 —— 项目规划的三种观点 《IF》 - Joseph Rudyard Kipling 论信息系统的架构设计 【转】系统架构设计师考试大纲(2009) nginx error_page 404 用 php header 无法跳转 The Elements of Programming Style 导出MySQL数据库结构的PHP脚本 轻松面试 搞定技术奇才 Cyworld Values & Principles 通过创建SCHEMABINDING VIEW提高JOIN的速度 长的很像Cyworld的QQ [北京][赛我网]HTML高级工程师 - guoadou - 博客园 [招聘] 韩国企业招聘Java .NET 开发人员 utf8_to_unicode in PHP document.body.scrollTop总是零的原因 [新闻]Cyworld.com.cn开始公测 hex2bin in PHP
unicode_to_utf8 in PHP
guoadou · 2005-09-26 · via 博客园 - guoadou

当短信格式为8:UCS2编码的时候,需要把Unicode的十六进制编码转换成UTF8,但是PHP没有内置Unicode的支持,怎么办呢?

/**
    * 把Unicode的十六进制字符串转换成utf8的文本字符串
    * @param mixed $str 要转换的字符串,不能为null
    * @return utf8的文本字符串
*/
public static function unicode_hex_to_utf8($str)    {
    
//判断长度
    if((strlen($str)%4!= 0 )
        
throw new exception('(strlen($str) % 4 != 0');
    
//计算byte[]的长度
    $len = strlen($str)/4;
    
$str_result = '';
    
//循环复制
    for($i=0;$i<$len;$i++){
        
$str_unicode_hex = substr($str, $i*4, 4);
        
$str_result .= self::unicode_to_utf8($str_unicode_hex);
    }
    
return $str_result;
}

private  static function unicode_to_utf8( $unicode_hex ) {

    
$unicode = hexdec($unicode_hex);

    
$utf8 = '';

    
if ( $unicode < 128 ) {
        
$utf8 = chr$unicode );

    } 
elseif ( $unicode < 2048 ) {
        
$utf8 .= chr192 + ( ( $unicode - ( $unicode % 64 ) ) / 64 ) );
        
$utf8 .= chr128 + ( $unicode % 64 ) );

    } 
else {

        
$utf8 .= chr224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
        
$utf8 .= chr128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) /
        
64 ) );
        
$utf8 .= chr128 + ( $unicode % 64 ) );

    } 
// if
    return $utf8;
// unicode_to_utf8