Home >Backend Development >Golang >How to Convert Integers to Binary in Go?
Converting Integers to Binary in Go
Converting numerical values to their binary representations is a common task in programming. Go offers a convenient built-in functionality within the strconv package to facilitate this process.
The strconv.FormatInt Function
The strconv.FormatInt function takes an int64 value and converts it to a string representation in the specified base. To convert to binary, specify the base as 2.
Example:
n := int64(123) fmt.Println(strconv.FormatInt(n, 2)) // 1111011
Output:
1111011
Further Information:
For more details on strconv.FormatInt, refer to the documentation:
http://golang.org/pkg/strconv/#FormatInt
Sample Code:
A simplified example using strconv.FormatInt is available at:
http://play.golang.org/p/leGVAELMhv
The above is the detailed content of How to Convert Integers to Binary in Go?. For more information, please follow other related articles on the PHP Chinese website!