Convert between slices of different types

前端 未结 7 1029
南旧
南旧 2020-11-30 01:35

I get a byte slice ([]byte) from a UDP socket and want to treat it as an integer slice ([]int32) without changing the underlying array, and vice ve

7条回答
  •  我在风中等你
    2020-11-30 02:21

    I had the size unknown problem and tweaked the previous unsafe method with the following code. given a byte slice b ...

    int32 slice is (*(*[]int)(Pointer(&b)))[:len(b)/4]
    

    The array to slice example may be given a fictional large constant and the slice bounds used in the same way since no array is allocated.

提交回复
热议问题