loop that will output in order from 1 to 5 []bytes in golang

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

loop that will output in order from 1 to 5 []bytes in golang

问题

如何创建一个循环,按顺序输出从1到5个字节的内容?

以下是我需要的输出:

  1. [0]
  2. [1]
  3. [2]
  4. ...
  5. [255]
  6. [0 1]
  7. [1 1]
  8. [2 1]
  9. ...
  10. 等等(最多5个字节)

例如,如果我使用以下函数将数字转换为字节,并进行正常的循环:

  1. for i := 0; i < 8589934590; i++ {
  2. b := intToBytes(i)
  3. fmt.Println(b)
  4. }
  5. func intToBytes(val int) []byte {
  6. r := make([]byte, 5)
  7. for i := int(0); i < 5; i++ {
  8. r[i] = byte((val >> (8 * i)) & 0xff)
  9. }
  10. return r
  11. }

输出的末尾会有额外的零。

如果要去除额外的零,可以尝试以下选项是否正确:

英文:

How to make a loop that will output in order from 1 to 5 []bytes?

Here is what I need in the output:

  1. [0]
  2. [1]
  3. [2]
  4. ...
  5. [255]
  6. [0 1]
  7. [1 1]
  8. [2 1]
  9. ...
  10. etc (to max 5 bytes)

For example, if I doing a normal loop from numbers and turn them into bytes using these functions:

  1. for i := 0; i &lt; 8589934590; i++ {
  2. b : intToBytes(i)
  3. fmt.Println(b)
  4. }
  5. func intToBytes(val int) []byte {
  6. r := make([]byte, 5)
  7. for i := int(0); i &lt; 5; i++ {
  8. r[i] = byte((val &gt;&gt; (8 * i)) &amp; 0xff)
  9. }
  10. return r
  11. }

there will be extra zeros at the end of the output.

If this option is correct, then how to get rid of extra zeros?

答案1

得分: 1

这是一个解决方案。使用append函数来更新字节切片的长度。当移位结果为零时停止。

  1. package main
  2. import "fmt"
  3. func intToBytes(val int) []byte {
  4. b := make([]byte, 0, 5)
  5. for i := range b[:cap(b)] {
  6. v := val >> (8 * i)
  7. if v == 0 && i != 0 {
  8. break
  9. }
  10. b = append(b, byte(v))
  11. }
  12. return b
  13. }
  14. func main() {
  15. for i := 0; i < 4; i++ {
  16. b := intToBytes(i)
  17. fmt.Println(b)
  18. }
  19. fmt.Println("...")
  20. for i := 260 - 8; i < 260; i++ {
  21. b := intToBytes(i)
  22. fmt.Println(b)
  23. }
  24. fmt.Println("...")
  25. for i := 8589934590 - 4; i < 8589934590; i++ {
  26. b := intToBytes(i)
  27. fmt.Println(b)
  28. }
  29. }

[0]
[1]
[2]
[3]
...
[252]
[253]
[254]
[255]
[0 1]
[1 1]
[2 1]
[3 1]
...
[250 255 255 255 1]
[251 255 255 255 1]
[252 255 255 255 1]
[253 255 255 255 1]

你可以在这里查看代码的运行结果:链接

英文:

Here is a solution. Use append to update the byte slice length. Stop when the shifted result is zero.

  1. package main
  2. import &quot;fmt&quot;
  3. func intToBytes(val int) []byte {
  4. b := make([]byte, 0, 5)
  5. for i := range b[:cap(b)] {
  6. v := val &gt;&gt; (8 * i)
  7. if v == 0 &amp;&amp; i != 0 {
  8. break
  9. }
  10. b = append(b, byte(v))
  11. }
  12. return b
  13. }
  14. func main() {
  15. for i := 0; i &lt; 4; i++ {
  16. b := intToBytes(i)
  17. fmt.Println(b)
  18. }
  19. fmt.Println(&quot;...&quot;)
  20. for i := 260 - 8; i &lt; 260; i++ {
  21. b := intToBytes(i)
  22. fmt.Println(b)
  23. }
  24. fmt.Println(&quot;...&quot;)
  25. for i := 8589934590 - 4; i &lt; 8589934590; i++ {
  26. b := intToBytes(i)
  27. fmt.Println(b)
  28. }
  29. }

https://go.dev/play/p/b91oYBpOw_Y

  1. [0]
  2. [1]
  3. [2]
  4. [3]
  5. ...
  6. [252]
  7. [253]
  8. [254]
  9. [255]
  10. [0 1]
  11. [1 1]
  12. [2 1]
  13. [3 1]
  14. ...
  15. [250 255 255 255 1]
  16. [251 255 255 255 1]
  17. [252 255 255 255 1]
  18. [253 255 255 255 1]

huangapple
  • 本文由 发表于 2022年2月18日 05:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/71165892.html
匿名

发表评论

匿名网友

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

确定