OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / vendor / github.com / bytom / common / bytes.go
diff --git a/vendor/github.com/bytom/common/bytes.go b/vendor/github.com/bytom/common/bytes.go
new file mode 100644 (file)
index 0000000..db0cdc9
--- /dev/null
@@ -0,0 +1,39 @@
+// Package common contains various helper functions.
+package common
+
+import (
+       "encoding/binary"
+       "encoding/hex"
+)
+
+func FromHex(s string) []byte {
+       if len(s) > 1 {
+               if s[0:2] == "0x" {
+                       s = s[2:]
+               }
+               if len(s)%2 == 1 {
+                       s = "0" + s
+               }
+               return Hex2Bytes(s)
+       }
+       return nil
+}
+
+func Bytes2Hex(d []byte) string {
+       return hex.EncodeToString(d)
+}
+
+func Hex2Bytes(str string) []byte {
+       h, _ := hex.DecodeString(str)
+       return h
+}
+
+func Unit64ToBytes(n uint64) []byte {
+       buf := make([]byte, 8)
+       binary.LittleEndian.PutUint64(buf, n)
+       return buf
+}
+
+func BytesToUnit64(b []byte) uint64 {
+       return binary.LittleEndian.Uint64(b)
+}