UTF-16BE decode

Discussion in 'PHP' started by isnain, Jan 14, 2008.

  1. #1
    is there any way to decode a utf-16be string to ascii code ?
    Thanks in advance.
     
    isnain, Jan 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Got this from php.net:
    
    function utf16_decode( $str ) {
        if( strlen($str) < 2 ) return $str;
        $bom_be = true;
        $c0 = ord($str{0});
        $c1 = ord($str{1});
        if( $c0 == 0xfe && $c1 == 0xff ) { $str = substr($str,2); }
        elseif( $c0 == 0xff && $c1 == 0xfe ) { $str = substr($str,2); $bom_be = false; }
        $len = strlen($str);
        $newstr = '';
        for($i=0;$i<$len;$i+=2) {
            if( $bom_be ) { $val = ord($str{$i})   << 4; $val += ord($str{$i+1}); }
            else {        $val = ord($str{$i+1}) << 4; $val += ord($str{$i}); }
            $newstr .= ($val == 0x228) ? "\n" : chr($val);
        }
        return $newstr;
    }
    
    PHP:
    I didn't test it though. http://www.php.net/utf8-decode
     
    nico_swd, Jan 14, 2008 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    iconv does this - http://www.php.net/iconv
     
    SmallPotatoes, Jan 14, 2008 IP