What is the equivalent of _byteswap_ulong in Golang? Does it exist as a package?
I tried to use the binary package and play with the reader, but couldn't get it working. I need to swap bytes in a uint64 variable.
Input is 2832779. Output should be 8b392b.
As @David said, use the binary.ByteOrder type
package main
import (
"encoding/binary"
"fmt"
)
func main() {
value := make([]byte, 4)
// need to know the byte ordering ahead of time
binary.LittleEndian.PutUint32(value, 0x12345678)
fmt.Printf("%#v
", value)
fmt.Printf("Big Endian Representation, 0x%X
", binary.BigEndian.Uint32(value))
fmt.Printf("Little Endian Representation, 0x%X
", binary.LittleEndian.Uint32(value))
}
This will output:
[]byte{0x78, 0x56, 0x34, 0x12}
Big Endian Representation, 0x78563412
Little Endian Representation, 0x12345678
These are the big-endian and little-endian representations on a little-endian server.
The package encoding/binary has a ByteOrder type http://golang.org/pkg/encoding/binary/#ByteOrder
binary.LittleEndian
and
binary.BigEndian
Let you swap to different orders.
It's not exactly the same as it doesn't just swap bytes. But may get you what you need.