Converting Numeric Values to Binary Strings in Go
Developers working with numerical data may encounter the need to represent numbers as binary strings. Go offers the strconv (string conversion) package for this purpose, providing the FormatInt function.
Problem:
Convert a numeric value (e.g., 123) to its binary representation (e.g., "1111011").
Solution:
package main import ( "fmt" "strconv" ) func main() { n := int64(123) binary := strconv.FormatInt(n, 2) fmt.Println(binary) // 1111011 }
Explanation:
The strconv.FormatInt function takes two arguments:
Demo:
You can try out the demo here: http://play.golang.org/p/leGVAELMhv
The above is the detailed content of How to Convert Numeric Values to Binary Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!