mirror of
https://github.com/fumiama/imago.git
synced 2026-06-11 12:10:29 +08:00
上传
This commit is contained in:
34
data.go
Normal file
34
data.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package imago
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Str2bytes Fast convert
|
||||
func Str2bytes(s string) []byte {
|
||||
x := (*[2]uintptr)(unsafe.Pointer(&s))
|
||||
h := [3]uintptr{x[0], x[1], x[1]}
|
||||
return *(*[]byte)(unsafe.Pointer(&h))
|
||||
}
|
||||
|
||||
// Bytes2str Fast convert
|
||||
func Bytes2str(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// bytes82uint64 字节数(大端)组转成int(无符号的)
|
||||
func bytes82uint64(b []byte) (uint64, error) {
|
||||
if len(b) == 9 {
|
||||
b = b[:7]
|
||||
}
|
||||
if len(b) == 8 {
|
||||
bytesBuffer := bytes.NewBuffer(b)
|
||||
var tmp uint64
|
||||
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
|
||||
return tmp, err
|
||||
}
|
||||
return 0, fmt.Errorf("%s", "bytes lenth is invaild!")
|
||||
}
|
||||
Reference in New Issue
Block a user