Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 387
Mike Fernandez Avatar asked Mar 15 '15 14:03

Mike Fernandez


People also ask

How to convert[] string to string in Go?

You can convert Golang “type []string” to string using string. Join() function, which takes two parameters. It returns a string.

What is uint8 go?

A uint8 is an unsigned integer, and can only have a positive value of 0 to 255. The ranges are based on the bit size. For binary data, 8 bits can represent a total of 256 different values.


1 Answers

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.

like image 174
Bayta Darell Avatar answered Oct 23 '22 15:10

Bayta Darell