1

Is it possible to convert UTF-16
U+610F
style character to UTF-8 (hex)
E6848F
using PHP ?

UTF-8 character is '意'

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Qiao
  • 16,565
  • 29
  • 90
  • 117

2 Answers2

1

From the comments in the chr man page, one quick hack for turning an ordinal character number into a UTF-8 byte sequence:

function unichr($u) {
    return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
}

// unichr(0x610F) -> "\xE6\x84\x8F"
bobince
  • 528,062
  • 107
  • 651
  • 834
  • I get utf8's '意', not "\xE6\x84\x8F". How did you get "\xE6\x84\x8F"? – Qiao Apr 20 '10 at 06:11
  • The byte string `"\xE6\x84\x8F"` (ie. the byte 0xE6, followed by the byte 0x84, then 0x8F), interpreted as UTF-8, is `意`. I put it in string literal escapes so you could see what you're really getting. – bobince Apr 20 '10 at 07:13
  • But i need get string `"\xE6\x84\x8F"`. Is it possible from this solution? – Qiao Apr 20 '10 at 07:18
  • 1
    The string `"\xE6\x84\x8F"` **is** `意`! Are you saying you want to see the actual hex codes of those bytes? Then use `bin2hex` on the string. http://php.net/manual/en/function.bin2hex.php – bobince Apr 20 '10 at 07:52
0

php have unicode encoding and decoding.. let u try on that

utf8_decode(); or utf8_encode();
Karthik
  • 3,221
  • 5
  • 28
  • 38