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

How to Convert Integers to Binary Representation in Go?

DDD
DDDOriginal
2024-11-18 06:21:02215browse

How to Convert Integers to Binary Representation in Go?

Converting Integers to Binary Representation in Go

Question:

Is there a built-in function in Go that can convert numeric types into their binary number form?

For instance, if we provide 123 as input, we should obtain the string "1111011" as output.

Answer:

Yes, there is an inbuilt function called strconv.FormatInt in the strconv package that can convert integers into their binary representation.

n := int64(123)

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

This FormatInt function takes two parameters: an int64 and a base. The base specifies the base of the numerical representation.

Example:

Documentation:

  • strconv package:
  • strconv.FormatInt function:

    • https://golang.org/pkg/strconv/#FormatInt

The above is the detailed content of How to Convert Integers to Binary Representation 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