patterngoCritical
Converting from an integer to its binary representation
Viewed 0 times
fromconvertingintegerrepresentationitsbinary
Problem
Has anyone got an idea if there is any inbuilt functionality in Go for converting from any one of the numeric types to its binary number form.
For example, if
For example, if
123 was the input, the string "1111011" would be the output.Solution
The
DEMO: http://play.golang.org/p/leGVAELMhv
http://golang.org/pkg/strconv/#FormatInt
FormatInt returns the string representation of i in the given base, for 2 = 10.
strconv package has FormatInt, which accepts an int64 and lets you specify the base.n := int64(123)
fmt.Println(strconv.FormatInt(n, 2)) // 1111011DEMO: http://play.golang.org/p/leGVAELMhv
http://golang.org/pkg/strconv/#FormatInt
func FormatInt(i int64, base int) stringFormatInt returns the string representation of i in the given base, for 2 = 10.
Code Snippets
n := int64(123)
fmt.Println(strconv.FormatInt(n, 2)) // 1111011Context
Stack Overflow Q#13870845, score: 160
Revisions (0)
No revisions yet.