Base64 can store 6 bits for each character used. Assuming we are using int64 or uint64 we use 64 bits, which could be represented in ~ 11 characters.
I've tried answer this question , but PHP fails to convert the values correctly.
$int = 5460109885665973483;
echo base64_encode($int);
Return:
NTQ2MDEwOTg4NTY2NTk3MzQ4Mw==
This is incorrect, we are using 26 characters to represent 64 bits! This is insane. I even understand the reason, it uses the value as a string, not as int. But the conversion to string does use 19 bytes, which therefore (19 * 8) / 6 characters are used by PHP.
However, other languages handle byte-level, such as Golang:
bt := make([]byte, 8)
binary.BigEndian.PutUint64(bt, 5460109885665973483)
fmt.Print(base64.StdEncoding.EncodeToString(bt))
Return:
S8Y1Axm4FOs=
The S8Y1Axm4FOs=
is exactly 11 characters (ignoring padding), which is exactly the 64-bit representation. In this case you can retrieve the value using binary.BigEndian.Uint64
after decode
Base64.
Which way could I get the same Golang result in PHP?