DEV Community

Tomasz Giba
Tomasz Giba

Posted on • Edited on

How get int from base64'd number bytes representation in Go

To make sure we won't overflow, we need math/big from Go

inputString := "Ag==" // base64'd byte representation of number 2

// first base64 -> bytes 
decodedBytes, _ := b64.StdEncoding.DecodeString(inputString)

// bytes -> big.Int
bigInt := new(big.Int)
bigInt.SetBytes(decodedBytes)

resultInt64 := bigInt.Int64() // == int64(2)
resultInt := int(resultInt64) // == int(2)
Enter fullscreen mode Exit fullscreen mode

https://play.golang.org/p/DVxwwJHrbNu

Top comments (0)