How to print a byte array []byte{255, 253}
as binary in Golang?
I.e.
[]byte{255, 253} --> 1111111111111101
The byte type in Golang is an alias for the unsigned integer 8 type ( uint8 ). The byte type is only used to semantically distinguish between an unsigned integer 8 and a byte. The range of a byte is 0 to 255 (same as uint8 ).
Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.
You can simply iterate the byte array and print the byte using System. out. println() method.
Go Binaries is an open-source server allowing non-Go users to quickly install tools written in Golang, without installing the Go compiler or a package manager — all you need is curl . Let's take a look at how it works, and how to use it!
Simplest way I have found:
package main
import "fmt"
func main() {
bs := []byte{0x00, 0xfd}
for _, n := range(bs) {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
}
Playground with this code: https://play.golang.org/p/eVez0vD4pJk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With