34 lines
958 B
Go
34 lines
958 B
Go
|
package main
|
||
|
|
||
|
import "unsafe"
|
||
|
import "reflect"
|
||
|
|
||
|
const BYTES_IN_INT32 = 4
|
||
|
const BYTES_IN_INT16 = 2
|
||
|
|
||
|
func UnsafeCaseInt32ToBytes(val int32) []byte {
|
||
|
hdr := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(&val)), Len: BYTES_IN_INT32, Cap: BYTES_IN_INT32}
|
||
|
return *(*[]byte)(unsafe.Pointer(&hdr))
|
||
|
|
||
|
}
|
||
|
|
||
|
func UnsafeCastInt32sToBytes(ints []int32) []byte {
|
||
|
length := len(ints) * BYTES_IN_INT32
|
||
|
hdr := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(&ints[0])), Len: length, Cap: length}
|
||
|
return *(*[]byte)(unsafe.Pointer(&hdr))
|
||
|
|
||
|
}
|
||
|
|
||
|
func UnsafeCaseInt16ToBytes(val int16) []byte {
|
||
|
hdr := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(&val)), Len: BYTES_IN_INT16, Cap: BYTES_IN_INT16}
|
||
|
return *(*[]byte)(unsafe.Pointer(&hdr))
|
||
|
|
||
|
}
|
||
|
|
||
|
func UnsafeCastInt16sToBytes(ints []int16) []byte {
|
||
|
length := len(ints) * BYTES_IN_INT16
|
||
|
hdr := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(&ints[0])), Len: length, Cap: length}
|
||
|
return *(*[]byte)(unsafe.Pointer(&hdr))
|
||
|
|
||
|
}
|