如何在Golang中将字节数组以二进制形式打印出来?

huangapple go评论122阅读模式
英文:

How to print a byte array as binary in golang?

问题

如何在Golang中将字节数组[]byte{255, 253}以二进制形式打印出来?

即:

  1. []byte{255, 253} --> 1111111111111101
英文:

How to print a byte array []byte{255, 253} as binary in Golang?

I.e.

  1. []byte{255, 253} --> 1111111111111101

答案1

得分: 21

我找到的最简单的方法是:

  1. package main
  2. import "fmt"
  3. func main() {
  4. bs := []byte{0x00, 0xfd}
  5. for _, n := range bs {
  6. fmt.Printf("%08b ", n) // 打印结果为 00000000 11111101
  7. }
  8. }

你可以在这个链接中的代码播放器中尝试这段代码:https://go.dev/play/p/piJV_3OTHae

英文:

Simplest way I have found:

  1. package main
  2. import "fmt"
  3. func main() {
  4. bs := []byte{0x00, 0xfd}
  5. for _, n := range(bs) {
  6. fmt.Printf("%08b ", n) // prints 00000000 11111101
  7. }
  8. }

Playground with this code: https://go.dev/play/p/piJV_3OTHae

答案2

得分: 1

使用这个简单版本的代码:

  1. func printAsBinary(bytes []byte) {
  2. for i := 0; i < len(bytes); i++ {
  3. for j := 0; j < 8; j++ {
  4. zeroOrOne := bytes[i] >> (7 - j) & 1
  5. fmt.Printf("%c", '0'+zeroOrOne)
  6. }
  7. fmt.Print(" ")
  8. }
  9. fmt.Println()
  10. }
  11. printAsBinary([]byte{0, 1, 127, 255}) // 输出:00000000 00000001 01111111 11111111

这段代码的功能是将字节切片转换为二进制表示形式并打印出来。给定的字节切片是[]byte{0, 1, 127, 255},它将被转换为二进制字符串00000000 00000001 01111111 11111111并打印出来。

英文:

Or use this simple version

  1. func printAsBinary(bytes []byte) {
  2. for i := 0; i &lt; len(bytes); i++ {
  3. for j := 0; j &lt; 8; j++ {
  4. zeroOrOne := bytes[i] &gt;&gt; (7 - j) &amp; 1
  5. fmt.Printf(&quot;%c&quot;, &#39;0&#39;+zeroOrOne)
  6. }
  7. fmt.Print(&quot; &quot;)
  8. }
  9. fmt.Println()
  10. }
  11. []byte{0, 1, 127, 255} --&gt; 00000000 00000001 01111111 11111111

答案3

得分: 0

fmt.Printf可以打印切片和数组,并将格式应用于数组的元素。这样可以避免循环,并且让我们非常接近目标:

  1. package main
  2. import "fmt"
  3. func main() {
  4. bs := []byte{0xff, 0xfd}
  5. fmt.Printf("%08b\n", bs) // 打印 [11111111 11111101]
  6. }

上述代码的 Playground 链接:https://go.dev/play/p/8RjBJFFI4vf

可以使用 strings.Trim() 去除额外的括号(这是切片表示法的一部分):

  1. fmt.Println(strings.Trim(fmt.Sprintf("%08b", bs), "[]")) // 打印 11111111 11111101
英文:

fmt.Printf can print slices and arrays and applies the format to the elements of the array. This avoids the loop and gets us remarkably close:

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. bs := []byte{0xff, 0xfd}
  5. fmt.Printf(&quot;%08b\n&quot;, bs) // prints [11111111 11111101]
  6. }

Playground with above code: https://go.dev/play/p/8RjBJFFI4vf

The extra brackets (which are part of the slice notation) can be removed with a strings.Trim():

  1. fmt.Println(strings.Trim(fmt.Sprintf(&quot;%08b&quot;, bs), &quot;[]&quot;)) // prints 11111111 11111101

huangapple
  • 本文由 发表于 2017年8月4日 02:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/45492153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定