5

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?

tonyl7126
  • 1,548
  • 3
  • 14
  • 19

1 Answers1

13

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.

Sebastian
  • 16,813
  • 4
  • 49
  • 56
  • 6
    The reason why this works is that [`big.Int` implements](http://golang.org/pkg/math/big/#Int.Format) the [`Formatter` interface](http://golang.org/pkg/fmt/#Formatter). – nemo Apr 27 '14 at 19:49
  • 1
    Actually, `*big.Int` (pointer type) implements the `Formatter` interface :) – Deleplace Aug 21 '15 at 13:58