Converting byte-stream into numeric data-type

后端 未结 4 734
不思量自难忘°
不思量自难忘° 2021-01-16 01:11

Let\'s say I have a byte-stream in which I know the location of a 64-bit value (a 64-bit nonce). The byte-order is Little-Endian. As PHP\'s integer data-type is limited to 3

4条回答
  •  青春惊慌失措
    2021-01-16 01:48

    This seems like a total hack, but it should do the job, assuming you have the BC Math functions that daemonmoi recommended:

    $result = "0";
    for ($i = strlen($serverChallenge) - 1; $i >= 0; $i--)
    {
        $result = bcmul($result, 256); // shift result
    
        $nextByte = (string)(ord($serverChallenge[$i]));
        $result = bcadd($result, $nextByte);
    }
    

提交回复
热议问题