Home > Enterprise >  How to convert bytes to integer in php like python libnum s2n function
How to convert bytes to integer in php like python libnum s2n function

Time:10-10

I Want To Convert Text To Number Like in Python. My Python Code:

from libnum import *
print(s2n("Text"))
# Output: 1415936116
print(s2n("\x31\x34\x31\x35\x39\x33\x36\x31\x31\x36"))
# Output: 1415936116

This is my php code. I went this far:

$String = "Text";
$Bytes = "";

$Length = strlen($String);
for ($i=0; $i<$Length; $i  ) {
    $Bytes .= "\x".(string)dechex(mb_ord($String[$i]));
}

echo $Bytes;
// Output: \x31\x34\x31\x35\x39\x33\x36\x31\x31\x36

CodePudding user response:

You can convert strings to an integer using hexdec(bin2hex($string));

I fiddled around trying to figure out how to turn the packed hex string into an integer but couldn't figure out how to return the same integer as the string.

  • Related