A
A
alex stephen2015-03-16 13:09:22
PHP
alex stephen, 2015-03-16 13:09:22

php: how to decode python hex string?

Hello.
I get a line like this from python code by exec:

\\xd0\\xa0\\xd1\\x83\\xd1\\x81\\xd1\\x81\\xd0\\xba\\xd0\\xb8\\xd0\\xb9

How to decrypt it in php?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-03-16
@berezuev

$input = '\\xd0\\xa0\\xd1\\x83\\xd1\\x81\\xd1\\x81\\xd0\\xba\\xd0\\xb8\\xd0\\xb9';
$input2 = "\xd0\xa0\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9";
$output = preg_replace_callback(
  '/\\\\x([a-f0-9]{2})/',
  function($match) {
    return pack('H*', $match[1]);
  },
  $input
);

var_dump($output, $input2 === $output);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question