将一个uint32数组写入一个字节片段并将其取回(Write an array of uint32 to a slice of bytes and obtain it back)

编程入门 行业动态 更新时间:2024-10-10 23:18:45
将一个uint32数组写入一个字节片段并将其取回(Write an array of uint32 to a slice of bytes and obtain it back)

我试图创建一个os.File的模拟或更具体的io.Reader 。 我想模拟读取128位具体数据而无需实际读取。

用一个uint32就没有问题。

嘲笑:

func (f *FileMock) Read(buf []byte) (n int, err error) { binary.BigEndian.PutUint32(buf, uint32(2052)) return len(buf), nil }

测试方法(简化):

b := make([]byte, 128) meta_data := make([]uint32, 4) _, err = s.Read(b) if err != nil { // Handle error } binary.Read(bytes.NewBuffer(b), binary.BigEndian, &meta_data) log.Print(meta_data) // Output [2052 0 0 0]

但是当我需要模拟阅读一个uint32切片时, PutUint32并没有什么帮助,因为它从开头写入切片(以前写过数据)。 我尝试了一堆bytes和binary工具的组合,但每次我都无法从字节获取数据时没有运气。 我有最后一次尝试(不是唯一的):

func (f *FileMock) Read(b []byte) (n int, err error) { buf := bytes.NewBuffer(make([]byte, len(b))) err = binary.Write(buf, binary.BigEndian, [4]uint32{2051, 123, 28, 28}) buf.Read(b) return len(b), nil }

使用与上述相同的测试方法,我正在获取空片[0, 0, 0, 0] 。 请注意,这是os.File.Read方法的模拟,所以我不能创建一个新的字节片段,而不是它,我需要将我的数据写入现有片段。

起初我想知道如何解决这个问题。 另外我想知道为什么只有[0, 0, 0, 0] ?

感谢您的回答!

I'm trying to create a mock for a os.File or to be more specific io.Reader. I want to emulate reading of 128 bits of concrete data without actual reading.

With a single uint32 there is no problems.

Mock:

func (f *FileMock) Read(buf []byte) (n int, err error) { binary.BigEndian.PutUint32(buf, uint32(2052)) return len(buf), nil }

Tested method (simplified):

b := make([]byte, 128) meta_data := make([]uint32, 4) _, err = s.Read(b) if err != nil { // Handle error } binary.Read(bytes.NewBuffer(b), binary.BigEndian, &meta_data) log.Print(meta_data) // Output [2052 0 0 0]

But when I need to mock reading of a uint32 slice PutUint32 is not helpful because it writes to a slice from beginning (overrites previously wrtten data). I tried a bunch of combinations of bytesand binary tools but had no luck every time I can't get the data back from bytes. There is my last attempt (it is not the only):

func (f *FileMock) Read(b []byte) (n int, err error) { buf := bytes.NewBuffer(make([]byte, len(b))) err = binary.Write(buf, binary.BigEndian, [4]uint32{2051, 123, 28, 28}) buf.Read(b) return len(b), nil }

With the same tested method as described above I'm obtaining an empty slice [0, 0, 0, 0]. Please note, this is a mock for os.File.Read method so I can't create a new slice of bytes instead of it I need to write my data to an existing slice.

At first I'm wondering how to solve the issue. Also I want to know why there is just [0, 0, 0, 0]?

Grateful for answers!

最满意答案

切片共享一个底层数组,你可以写(PutUint32)到buf [4:],buf [8:]等。

Slices share an underlying array, you can write (PutUint32) to buf[4:], buf[8:] etc.

更多推荐

本文发布于:2023-08-08 00:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字节   片段   并将其   obtain

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!