Sobes.tech
Senior

How to convert a number to the binary system?

sobes.tech AI

Answer from AI

In Go, to convert a number to binary, you can use the FormatInt function from the strconv package. It takes the number and the base of the numeral system.

Example:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var num int64 = 42
	binary := strconv.FormatInt(num, 2)
	fmt.Println(binary) // Outputs: 101010
}

If the number is unsigned (uint), you can use FormatUint similarly.

How to convert a number to the binary system? — Golang - sobes.tech