Unicode to UTF8 and vice versa
Posted: Wed Jul 13, 2005 4:38 am
Just something i have searched alot and would like to share
Here is a javascript code for conversion of UTF-8 to Unicode
and to reconvert it here is the code in PHP
Here is a javascript code for conversion of UTF-8 to Unicode
Code: Select all
<script type="e;text/javascript"e;>
<!--
function unicode()
{
var src = document.budgetdetail.T1.value;
var srcLength = src.length;
var i, c, unicodeString = '';
var zeros = "e;0000"e;;
for (i = 0; i < srcLength; i++) {
c = src.charCodeAt(i).toString(16);
unicodeString += '%u' + zeros.substring(c.length) + c;
}
return unicodeString;
}
//-->
</script>Code: Select all
function uni2utf8($uniescape)
{
$c = "";
$n = intval(substr($uniescape, -4), 16);
if ($n < 0x7F) { // 0000-007F
$c .= chr($n);
} elseif ($n < 0x800) { // 0080-0800
$c .= chr(0xC0 | ($n / 64));
$c .= chr(0x80 | ($n % 64));
} else { // 0800-FFFF
$c .= chr(0xE0 | (($n / 64) / 64));
$c .= chr(0x80 | (($n / 64) % 64));
$c .= chr(0x80 | ($n % 64));
}
return $c;
}