Reputation: 11
I have a windows 1251 encoded string that needs to be converted to HEX or binary (but if I manage to convert it to one of the two, I'll be able to convert it to the other). The string comes from a cash register so there is nothing I can do about the initial encoding.
So far I've tried to convert it to UTF-8, then to bin and then to hex but at the end it's just not right. The string is "Ђ ЂЂ„‚" which in hex is 80 20 80 80 84 82 and in binary it should be 10000000 10100000 10000000 10000000 10000100 10000010
I think that my first problem is the win-1251 to utf-8 conversation.
$decodedUtf8 = mb_convert_encoding($decoded, "utf-8", "windows-1251");
The result from this is 'Ђ ЂЂ„‚' -> 'Ђ ЂЂ„‚', and I'm not experienced with encoding but this one looks suspicious.
So far my whole code is that:
public function decodeStatus($encodedStr){
$decoded = base64_decode($encodedStr);
//$iconverted = iconv('cp1251', 'utf-8', $decoded);
$decodedUtf8 = mb_convert_encoding($decoded, "utf-8", "windows-1251");
echo $decodedUtf8;
$chars = str_split($decodedUtf8);
foreach ($chars as $char){
$bits[] = decbin(ord($char));
}
return $bits;
}
The final array should consist of 6 elements like this:
[
1000000,
10100000,
10000000,
10000000,
10000100,
10000010
]
Is there a way to convert it only with PHP? And can you give me some directions how to do it?
Upvotes: 1
Views: 438
Reputation: 7703
The origin / encoding is irrelevant for the conversion of a (bin)string into a HEX string. This is another notation for your string:
$inputString = "\x80\x20\x80\x80\x84\x82";
Use for a conversion to Hexadecimal bin2hex:
$hexString = bin2hex($inputString);
var_dump($hexString);
//string(12) "802080808482"
Edit: To convert to Bin you can use your script without the encode/decode functons. Another variant:
$input = "\xd0\x82\xc2\xa0\xd0\x82\xd0\x82\xe2\x80\x9e\xe2\x80\x9a";
var_dump(bin2hex($input)); //string(28) "d082c2a0d082d082e2809ee2809a"
$binArray = [];
for($i=0; $i<strlen($input);$i++){
$binArray[] = sprintf("%08b",ord($input[$i]));
}
var_dump($binArray);
Upvotes: 0