note 49171 modified in function.iconv by yannick

From: Date: Thu, 18 Mar 2010 22:50:25 +0000
Subject: note 49171 modified in function.iconv by yannick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167181@lists.php.net to get a copy of this message
Here is how to convert UCS-2 numbers to UTF-8 numbers in hex: <?php function ucs2toutf8($str) { for ($i=0;$i<strlen($str);$i+=4) { $substring1 = $str[$i].$str[$i+1]; $substring2 = $str[$i+2].$str[$i+3]; if ($substring1 == "00") { $byte1 = ""; $byte2 = $substring2; } else { $substring = $substring1.$substring2; $byte1 = dechex(192+(hexdec($substring)/64)); $byte2 = dechex(128+(hexdec($substring)%64)); } $utf8 .= $byte1.$byte2; } return $utf8; } echo strtoupper(ucs2toutf8("06450631062D0020")); ?> Input: 06450631062D Output: D985D8B1D8AD regards, Ziyad --was-- Here is how to convert UCS-2 numbers to UTF-8 numbers in hex: function ucs2toutf8($str) { for ($i=0;$i<strlen($str);$i+=4) { $substring1 = $str[$i].$str[$i+1]; $substring2 = $str[$i+2].$str[$i+3]; if ($substring1 == "00") { $byte1 = ""; $byte2 = $substring2; } else { $substring = $substring1.$substring2; $byte1 = dechex(192+(hexdec($substring)/64)); $byte2 = dechex(128+(hexdec($substring)%64)); } $utf8 .= $byte1.$byte2; } return $utf8; } echo strtoupper(ucs2toutf8("06450631062D0020")); ?> Input: 06450631062D Output: D985D8B1D8AD regards, Ziyad http://php.net/manual/en/function.iconv.php

« previous php.notes (#167181) next »