Convert (doublebyte) strings to hexadecimal

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

Русский 

My question is: how to write a function that will go from "Russian" in Cyrillic to a hexadecimal value, as described above? Can the same function work for single byte characters?

+4
source share
2 answers

Objects 〹 are called HTML entities. PHP has a function that can create them: mb_encode_numericentity Docs , this is 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 chose UTF-8 , depending on this you need to change the $encoding parameter of the function and the $convmap .

+5
source

Your provided example is not hexadecimal, but if you want to convert to hex, try the following:

 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 
+2
source

Source: https://habr.com/ru/post/915759/


All Articles