tonyl7126
tonyl7126

Reputation: 1568

Golang Big Integers to Binary String

I see it's simple to convert golang's big int (math/big package) to a string, but is there any straightforward way of converting a big int to a binary string?

Upvotes: 5

Views: 3623

Answers (2)

Chakshu Jain
Chakshu Jain

Reputation: 257

Use Text function

num := big.NewInt(8)
fmt.Println(num.Text(2)) // 1000

Upvotes: 0

Sebastian
Sebastian

Reputation: 17433

Should be as easy as this:

i := big.NewInt(2014)
s := fmt.Sprintf("%b", i) // 11111011110

fmt.Println(s)

Hope this is what you are looking for.

Upvotes: 13

Related Questions