Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert int64 to binary and keep leading zeros in golang?

Tags:

go

I have created a program which converts int64 to binary:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    n := int64(3)
    fmt.Println(strconv.FormatInt(n, 2))
}

And it returns this value:

11

How can I keep the leading zeros in the answer?

Thanks in advance!

like image 515
user1933049 Avatar asked Oct 16 '25 12:10

user1933049


1 Answers

You can format directly as binary with padding:

fmt.Printf("%064b\n", n)

See https://play.golang.org/p/JHCgyPMKDG

like image 83
coredump Avatar answered Oct 19 '25 00:10

coredump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!