golang: convert uint32 (or any built-in type) to []byte (to be written in a file)

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

golang: convert uint32 (or any built-in type) to []byte (to be written in a file)

问题

我正在尝试使用Go语言中的unsafe库将_uint32_转换为字节数组(4个字节):

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := make([]byte, unsafe.Sizeof(h))
copy(a, *(*[]byte)(unsafe.Pointer(&h)))

前两行是正确的,但是在_copy_调用处出现了运行时错误(unexpected fault address)。

下一步是调用_Write_函数将这4个字节写入文件:

_, err = fi.Write(a)

我找到了其他类似主题的问题,但没有一个有可用的代码。
我也知道unsafe是不安全的。

非常感谢任何帮助。

英文:

I'm trying to convert an uint32 to a byte array (4 bytes) in Go using the unsafe library:

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := make([]byte, unsafe.Sizeof(h))
copy(a, *(*[]byte)(unsafe.Pointer(&h)))

The first two lines are correct, but then I get a runtime error ( unexpected fault address ) at the copy call.

The next step would be to call Write

_, err = fi.Write(a)

to write the 4 bytes into a file.

I've found other questions with a similar topic, but none with a working code.
I'm also aware that unsafe is unsafe.

Any help would be greatly appreciated.

答案1

得分: 20

避免使用unsafe包。

使用encoding/binary包将uint32转换为字节切片:

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := make([]byte, 4)
binary.LittleEndian.PutUint32(a, h)
_, err = fi.Write(a)

这个一行代码也可以实现相同的功能,但会增加额外的运行时开销:

err := binary.Write(fi, binary.LittleEndian, (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h))

以下是使用unsafe包进行转换的方法:

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := (*[4]byte)(unsafe.Pointer(&h))[:]
_, err = fi.Write(a)

表达式(*[4]byte)(unsafe.Pointer(&h))uint32指针转换为[4]byte指针。末尾的[:]创建了一个[4]byte的切片。

问题中的代码将uint32解释为slice header。生成的切片是无效的,copy操作会出错。

英文:

Avoid the unsafe package.

Use the encoding/binary package to convert a uint32 to a slice of bytes:

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := make([]byte, 4)
binary.LittleEndian.PutUint32(a, h)
_, err = fi.Write(a)

This one-liner does the same thing, but has an additional runtime cost:

err := binary.Write(fi, binary.LittleEndian, (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h))

Here's how to do the conversion with the unsafe package:

h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := (*[4]byte)(unsafe.Pointer(&h))[:]
_, err = fi.Write(a)

The expression (*[4]byte)(unsafe.Pointer(&h)) converts a uint32 pointer to a [4]byte pointer. The [:] at the end creates a slice on the [4]byte.

The code in the question interprets the uint32 as a slice header. The resulting slice is not valid and copy faults.

huangapple
  • 本文由 发表于 2015年3月15日 22:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/29061764.html
匿名

发表评论

匿名网友

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

确定