javascript – Convert string to devnagari unicode-ThrowExceptions

Exception or error:

I am trying to get the Devnagari Unicode of a given string.

https://codepoints.net/U+0924

I have string = “

How do I get its Unicode value (\u0924)?

I would like to get it in php or javascript.

Update

Also, string = “tkfO{“. This should return \u0924\u092A\u093E\u0908.

  1. https://codepoints.net/U+0924
  2. https://codepoints.net/U+092A
  3. https://codepoints.net/U+093E
  4. https://codepoints.net/U+0908
How to solve:

use mb_ord and than dechex

print dechex(mb_ord("त"));

Answer:

Here’s a way in PHP:

function encodeString($str)
{
    $s = iconv('UTF-8', 'UTF-32BE', $str);
    $nb = strlen($s) / 4;
    $res = '';
    for($i=0;$i<$nb;$i++)
        $res .= encodeChar(substr($s, 4*$i, 4));
    return $res;
}

function encodeChar($c)
{
    $s = bin2hex($c);
    while(substr($s,0,2)=='00')
        $s = substr($s, 2);
    return '\\u'.$s;
}

echo encodeString('तपाई');

Output: \u0924\u092a\u093e\u0908

Leave a Reply

Your email address will not be published. Required fields are marked *