loading...

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

tomaszgiba profile image Tomasz Giba Updated on ・1 min read

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

Discussion

pic
Editor guide