2013-01-02

PHP: Integer to UTF-8 String conversion

Still working with my hieroglyphs; I needed a simple method to convert integer values to UTF-8 string for output as ideograms. Here is a very simple method to do this:

function uft8_chr($i) {
   return mb_convert_encoding(pack('N',$i),'UTF-8','UTF-32BE');
}

A more direct approach to the conversion looks like this:

function utf8_ord($ch) {
   $len = strlen($ch);
   if($len<=0) return false;
   $h = ord($ch[0]);
   if($h<=0x7F) return $h;
   if($h<0xC2) return false;
   if($h<=0xDF && $len>1) return ($h&0x1F)<<6|(ord($ch[1])&0x3F);
   if($h<=0xEF && $len>2) return ($h&0x0F)<<12|(ord($ch[1])&0x3F)<<6|(ord($ch[2])&0x3F);        
   if($h<=0xF4 && $len>3) return ($h&0x0F)<<18|(ord($ch[1])&0x3F)<<12|(ord($ch[2])&0x3F)<<6|(ord($ch[3])&0x3F);
   return false;
}

function utf8_chr($num) {
   if($num<0x80) return chr($num);
   if($num<0x800) return chr(($num>>6)+0xC0).chr(($num&0x3F)+0x80);
   if($num<0x10000) return chr(($num>>12)+0xE0).chr((($num>>6)&0x3F)+0x80).chr(($num&0x3F)+0x80);
   if($num<0x200000) return chr(($num>>18)+0xF0).chr((($num>>12)&0x3F)+0x80).chr((($num>>6)&0x3F)+0x80).chr(($num&0x3F)+0x80);
   return false;
}