'; print_r(bindec("11111111")); Result - 131313...">

How to convert 8-bit binary data to hexadecimal

print_r(bin2hex("11111111"));
echo '</br>';
print_r(bindec("11111111"));

Result

  • 13131313131313131
  • 255

I want a hexadecimal 16 byte value to do aes encryption. How conversion from binary to hex happens in php. I get the wrong value with a function. Also, when I convert an array of hexadecimal values ​​to align the change byte length

+4
source share
3 answers

You get the right result, it's just not what you want. bin2hex()returns an ASCII string of hexadecimal representation. Quote from the manual:

Returns an ASCII string containing the hexadecimal representation of str.

, , :

print_r(dechex(bindec("11111111")));
+1

hexidecimal dechex(), . , bindec(), dechex(), :

print_r(dechex(bindec("11111111")));
+1
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>

PHP.NET: http://php.net/manual/en/function.bin2hex.php

: http://www.cs.princeton.edu/courses/archive/fall07/cos109/bc.html

: http://www.computerhope.com/binhex.htm

9 * 16 + F, F 15 ( A F 10 15). , 0x9F 159.

314,159:

  3 * 100,000 (10^5, "to the power of", not "xor")
+ 1 *  10,000 (10^4)
+ 4 *   1,000 (10^3)
+ 1 *     100 (10^2)
+ 5 *      10 (10^1)
+ 9 *       1 (10^0)
for decimal (base 10).

- " " . 159 ( 8 ) , .

+1

All Articles