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

How to Convert Integers to Binary in Go?

Barbara Streisand
Release: 2024-11-13 05:32:02
Original
401 people have browsed it

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
Copy after login

Output:

1111011
Copy after login

Further Information:

For more details on strconv.FormatInt, refer to the documentation:

http://golang.org/pkg/strconv/#FormatInt
Copy after login

Sample Code:

A simplified example using strconv.FormatInt is available at:

http://play.golang.org/p/leGVAELMhv
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template