doudun5009 2019-08-13 19:33
浏览 256
已采纳

遍历字节数组的所有值

I'm trying to loop through all values of a small byte array (64 bytes). I want to increment the way a digital clock would, start at index 0, go 0-255 then increment index 1, repeat. I know this is probably a recursion method, but I can't wrap my head around the logic.

So basically, for a 4 byte array (smaller for convenience)

Round 1

value: [0 0 0 0]

Round 256

value [ 255 0 0 0]

Round 256

value [ 0 1 0 0 ]

Round 257

value [ 1 1 0 0 ]

What I have so far:

func allValues() {
    currentPlaceIndex := 0
    content := make([]uint8, 64)
    max := 256

    for i := 0; i < max; i++ {
        content[currentPlaceIndex] = uint8(i)
    }
    fmt.Println(content)
    content[currentPlaceIndex] = 0

}

I can't seem to figure out how to recurse this.

  • 写回答

1条回答 默认 最新

  • dongzhuohan7085 2019-08-13 19:45
    关注

    Converting round number to byte array

    This representation is basically the base 256 radix. The first byte will be the round number % 256. Then divide the round number by 256, and repeat until the round number becomes 0.

    Since 256 is a special number (256 = 28), you may use bitwise operations for the calculations. E.g. the remainder is equal to bitmasking with 0xff, and the division by 256 is equal to shifting right by 8.

    Also note that there is a big.Int type in the standard library which represents a (signed) multi-precision integer. And it has an Int.Bytes() method which returns the absolute value as a big-endian byte slice. This is "almost" what we want, it's just in different byte order. So you may simply convert a round number to big.Int (e.g. using the Int.SetString() method), get its byte slice and reverse it.

    Iterating over the values

    If you don't want to convert a single round number but just iterate over the subsequent values, then do it like this:

    1. You start with a full zero slice or array.

    2. Check the first element of the slice. If < 255, simply increment it by one. Done with the iteration.

    3. If it was 255, zero it, and attempt to do the same with the 2nd element: go to step 2.

    So if you have a state, this is how you can calculate the next one:

    func next(data []byte) {
        for idx := range data {
            if data[idx] < 255 {
                data[idx]++
                return
            }
            data[idx] = 0
        }
        fmt.Println("overflow")
    }
    

    This is how you can test it:

    data := make([]byte, 64)
    for iter := 0; iter < 600; iter++ {
        next(data)
        fmt.Println(data)
    }
    

    This will output (try it on the Go Playground):

    [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    [2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程
  • ¥15 arduino esp8266开发
  • ¥15 stm32单片机通过485发送命令给驱动器控制电机转动,同样的代码f103可以控制电机转动,换到f407不能动了,但是用串口助手调试407显示发送的命令都是正确的,卡了好久了这是发送规则