OSDN Git Service

fix log (#388)
[bytom/vapor.git] / common / bytes.go
1 // Package common contains various helper functions.
2 package common
3
4 import (
5         "encoding/binary"
6         "encoding/hex"
7 )
8
9 func FromHex(s string) []byte {
10         if len(s) > 1 {
11                 if s[0:2] == "0x" {
12                         s = s[2:]
13                 }
14                 if len(s)%2 == 1 {
15                         s = "0" + s
16                 }
17                 return Hex2Bytes(s)
18         }
19         return nil
20 }
21
22 func Bytes2Hex(d []byte) string {
23         return hex.EncodeToString(d)
24 }
25
26 func Hex2Bytes(str string) []byte {
27         h, _ := hex.DecodeString(str)
28         return h
29 }
30
31 func Unit64ToBytes(n uint64) []byte {
32         buf := make([]byte, 8)
33         binary.LittleEndian.PutUint64(buf, n)
34         return buf
35 }
36
37 func BytesToUnit64(b []byte) uint64 {
38         return binary.LittleEndian.Uint64(b)
39 }