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?
Asked
Active
Viewed 3,286 times
5
-
What is a binary string? – Paul Hankin Apr 27 '14 at 07:30
-
@Anonymous A string of the form 10110101011010010101010 I guess. – fuz Apr 27 '14 at 09:11
1 Answers
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
-
6The 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
-
1Actually, `*big.Int` (pointer type) implements the `Formatter` interface :) – Deleplace Aug 21 '15 at 13:58