遍历字节数组的所有值

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.

dtgv52982
dtgv52982 那就从现在开始吧!jk...看来您已经回答了我应该一直问的问题。
大约一年之前 回复
douchao1879
douchao1879 根据我的计算,此数组可能处于256^64=2^512个可能的状态。我不认为您可以在宇宙热死之前现实地遍历所有这些组合。
大约一年之前 回复

1个回答

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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0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 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]
[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]
...
duanjie6912
duanjie6912 第一部分是如果您要将一个任意的整数转换为该字节片。 如果您只想顺序生成它们,那么第二部分将对此进行介绍。
大约一年之前 回复
duanchangnie7996
duanchangnie7996 我以前从未做过按位运算。 那是更好的方法吗?
大约一年之前 回复
dongyan7172
dongyan7172 那只是表示的意思。 但是这种“数据结构”基本上只是一个字节片:[] byte。
大约一年之前 回复
douqian2957
douqian2957 您输入答案的第一件事...我就是这么想的。 我不确定是否可以创建自己的数据类型? 以256为底的整数?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐