1

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?

happybit
  • 11
  • 2

1 Answers1

0

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);
jspit
  • 7,276
  • 1
  • 9
  • 17
  • I'm not sure how to convert my string to "This is another notation for your string". Some help with that? If I try to convert it directly with ```bin2hex``` I get "d082c2a0d082d082e2809ee2809a" which is not what I need. – happybit Aug 28 '19 at 09:03
  • if you get with bin2hex this "d082c2a0d082d082e2809ee2809a" is the $inputString = "\xd0\x82\xc2\xa0\xd0\x82\xd0\x82\xe2\x80\x9e\xe2\x80\x9a"; . var_dump(bin2hex($inputString) === "d082c2a0d082d082e2809ee2809a"); //bool(true) – jspit Aug 28 '19 at 11:55
  • And "d082c2a0d082d082e2809ee2809a" is not what I need. Therefore I think there is some error with the windows-1251 encoding thing. In your first example you hardcoded the $inputString in this format. The problem is that initially I have this - "Ђ ЂЂ„‚" – happybit Aug 30 '19 at 15:03
  • The important part of what @jspit said is "use your script without the encode/decode functons". @happybit in your code, change `$chars = str_split($decodedUtf8);` to `$chars = str_split($decoded);` and remove everything about `$decodedUtf8`. – whydoubt Aug 30 '19 at 15:45