About Me: Jawaad Mahmood: 30, Muslim, Canadian, freelance, doing consulting in Tokyo, Japan.

PHP: Convert UTF-8 to Hex Codepoint values (Unicode Hexidecimal)

January 16, 2008

This is one of those strange things that sounds a lot easier to do than it is.

I originally handled this by exploding the character through this:

$array = preg_split('//u', $a);

This worked fine and the string was split into an array of unicode characters. The next part was converting it into a useful hexidecimal value.

$character = $array[0];
$value = hexdec(bin2hex($character));

I originally thought this was the way to do so – I was wrong, don’t do it. It turns out there is no real simple way to convert from UTF-8 to hex values. Instead, try the UTF8ToUnicode function here: http://hsivonen.iki.fi/php-utf8/

Include this function and use the author’s utf8ToUnicode function. It becomes simple then:

$value = utf8ToUnicode($character);
$value = $value[0];

I am only posting this because of the sheer amount of time it took for me to find this information. I hope it helps you out.

Comments (1)

One Response to “PHP: Convert UTF-8 to Hex Codepoint values (Unicode Hexidecimal)”

  1. Simon says:

    Thanks for this!

    I’d already found Henri Sivonen’s code, but your tale of success encouraged me to reassess it, after initally dismissing it.

    I did have to convert the result to hex with dechex($value) at the end, and for some reason I also had to modify the utf8ToUnicode() function to accept the variable passed by value, but I now have everything I need.

    Thanks again for posting!


Archives 
August 2010 July 2010 May 2010 April 2010 March 2010 February 2010 January 2010 December 2009 November 2009 October 2009 August 2009 July 2009 June 2009 May 2009 April 2009 March 2009 February 2009 January 2009 December 2008 November 2008 August 2008 July 2008 June 2008 May 2008 April 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 February 2005 December 2004 November 2004 October 2004 September 2004

Creative Commons License
All text and images under 1024 x 768 pixels on this site are licensed under a Creative Commons Attribution 3.0 Unported License.
I require a link back to the original page of the article/image. If the image/article
is not public (IE: It is not present in an article on the blog), you may not reproduce it without permission.