4

Let's say I have the word "Russian" written in Cyrillic. This is would be the quivalent of the following in Hex:

Русский

My question is: how do I write a function which will go from "Russian" in Cyrillic to it's hex value as above? Could this same function work also for singel byte characters?

5
  • Are you looking for htmlspecialchars()? Commented Aug 10, 2011 at 17:05
  • That won't convert every letter, only applicable special characters. Commented Aug 10, 2011 at 17:06
  • @brad, @alien: use htmlentities() which should convert most characters Commented Aug 10, 2011 at 17:12
  • Doesn't work with cryllic: echo htmlentities('русский'); // outputs руÑÑкий Commented Aug 10, 2011 at 17:14
  • 1
    htmlentities converts nothing of those, use mb_encode_numericentity. Commented Aug 10, 2011 at 17:34

2 Answers 2

5

The 〹 thingies are called HTML Entities. In PHP there is a function that can create these: mb_encode_numericentityDocs, it's part of the Multibyte String extension (Demo):

$cyrillic = 'русский';

$encoding = 'UTF-8';
$convmap = array(0, 0xffff, 0, 0xffff);
$encoded = mb_encode_numericentity($cyrillic, $convmap, $encoding);

echo $encoded; # русский

However: You need to know the encoding of your Cyrillic string. In this case I've chosen UTF-8, depending on it you need to modify the $encoding parameter of the function and the $convmap array.

Sign up to request clarification or add additional context in comments.

Comments

2

Your provided example isn't hex, but if you want to convert to hex, try this:

function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

echo strToHex('русский'); // d180d183d181d181d0bad0b8d0b9

1 Comment

One note - control characters (such as \n) cause issue, so you'll need to 0-pad them by changing the " dechex(ord($string[$i])) " bit to " str_pad(dechex(ord($string[$i])), 2, "0", STR_PAD_LEFT) " in the strToHex() call. But a fantastic answer overall - thanks very much for this :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.