base64 encoding of binary value of 64 bit integer in PHP is not working

Viewed 2181

How to use base64 encoding with binary string of 64 bit integer on PHP?

This code is not working as expected

<?PHP
$t=11545152599186258990;
$byte_array_t = pack('P',$t);
echo base64_encode($byte_array_t); //not correct result - it should be: LrwswB6fOKA=
echo '
';
$t=11;
$byte_array_t = pack('P',$t);
echo base64_encode($byte_array_t); //correct

I am doing this because I am trying to implement the below code (golang) in PHP:

package main
import (
"fmt"
"encoding/base64"
"encoding/binary"
)



func main()  {

    dst := make([]byte, 8)
    binary.LittleEndian.PutUint64(dst, uint64(11545152599186258990))
    value :=base64.URLEncoding.EncodeToString(dst)
    fmt.Println(value)



}
3 Answers
Related