Home  >  Article  >  Backend Development  >  How to Convert Integers to Binary in Go?

How to Convert Integers to Binary in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-11-13 05:32:02331browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn