package binary import ( "encoding/binary" "fmt" "strconv" "strings" ) // StrToBytes converts string("00 00 00 00 00 00 00 00") to []byte func StrToBytes(str string) []byte { bytes := make([]byte, 8) for i, e := range strings.Fields(str) { b, _ := strconv.ParseUint(e, 16, 64) bytes[i] = byte(b) } return bytes } // BytesToStr converts []byte to string("00 00 00 00 00 00 00 00") func BytesToStr(bytes ...byte) string { strs := []string{} for _, b := range bytes { strs = append(strs, fmt.Sprintf("%02x", b)) } return strings.Join(strs, " ") } // BytesToUInt converts []byte to uint64 func BytesToUInt(bytes ...byte) uint64 { padding := make([]byte, 8-len(bytes)) i := binary.BigEndian.Uint64(append(padding, bytes...)) return i } // UintToBytes converts uint64 to []byte func UintToBytes(i uint64, size int) []byte { bytes := make([]byte, 8) binary.BigEndian.PutUint64(bytes, i) return bytes[8-size : 8] } // BytesToInt converts []byte to int64 func BytesToInt(bytes ...byte) int64 { if 0x7f < bytes[0] { mask := uint64(1<