Converting from an integer to its binary representation

cobie picture cobie · Dec 14, 2012 · Viewed 44.1k times · Source

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 123 was the input, the string "1111011" would be the output.

Answer

I Hate Lazy picture I Hate Lazy · Dec 14, 2012

The strconv package has FormatInt, which accepts an int64 and lets you specify the base.

n := int64(123)

fmt.Println(strconv.FormatInt(n, 2)) // 1111011

DEMO: