github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/common/byteutil/byteutil.go (about)

     1  // create by platon
     2  package byteutil
     3  
     4  import (
     5  	"bytes"
     6  	"encoding/binary"
     7  	"github.com/PlatONnetwork/PlatON-Go/common"
     8  	"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
     9  	"math/big"
    10  	"strings"
    11  )
    12  
    13  var Command = map[string]interface{}{
    14  	"string":            BytesToString,
    15  	"[]uint8":           OriginBytes,
    16  	"[64]uint8":         BytesTo64Bytes,
    17  	"[32]uint8":         BytesTo32Bytes,
    18  	"int":               BytesToInt,
    19  	"*big.Int":          BytesToBigInt,
    20  	"uint32":            BytesTouint32,
    21  	"uint64":            BytesTouint64,
    22  	"int32":             common.BytesToInt32,
    23  	"int64":             common.BytesToInt64,
    24  	"float32":           common.BytesToFloat32,
    25  	"float64":           common.BytesToFloat64,
    26  	"discover.NodeID":   BytesToNodeId,
    27  	"[]discover.NodeID": ArrBytesToNodeId,
    28  	"common.Hash":       BytesToHash,
    29  	"[]common.Hash":     ArrBytesToHash,
    30  	"common.Address":    BytesToAddress,
    31  }
    32  
    33  func BytesToAddress(curByte []byte) common.Address {
    34  	str := BytesToString(curByte)
    35  	return common.HexToAddress(str)
    36  }
    37  
    38  func BytesToNodeId(curByte []byte) discover.NodeID {
    39  	str := BytesToString(curByte)
    40  	nodeId, _ := discover.HexID(str)
    41  	return nodeId
    42  }
    43  
    44  func BytesToHash(curByte []byte) common.Hash {
    45  	str := BytesToString(curByte)
    46  	return common.HexToHash(str)
    47  }
    48  
    49  func ArrBytesToHash(curByte []byte) []common.Hash {
    50  	str := BytesToString(curByte)
    51  	strArr := strings.Split(str, ":")
    52  	var AHash []common.Hash
    53  	for i := 0; i < len(strArr); i++ {
    54  		AHash = append(AHash, common.HexToHash(strArr[i]))
    55  	}
    56  	return AHash
    57  }
    58  
    59  func ArrBytesToNodeId(curByte []byte) []discover.NodeID {
    60  	str := BytesToString(curByte)
    61  	strArr := strings.Split(str, ":")
    62  	var ANodeID []discover.NodeID
    63  	for i := 0; i < len(strArr); i++ {
    64  		nodeId, _ := discover.HexID(strArr[i])
    65  		ANodeID = append(ANodeID, nodeId)
    66  	}
    67  	return ANodeID
    68  }
    69  
    70  func BytesTo32Bytes(curByte []byte) [32]byte {
    71  	var arr [32]byte
    72  	copy(arr[:], curByte)
    73  	return arr
    74  }
    75  
    76  func BytesTo64Bytes(curByte []byte) [64]byte {
    77  	var arr [64]byte
    78  	copy(arr[:], curByte)
    79  	return arr
    80  }
    81  
    82  func OriginBytes(curByte []byte) []byte {
    83  	return curByte
    84  }
    85  
    86  func BytesToBigInt(curByte []byte) *big.Int {
    87  	return new(big.Int).SetBytes(curByte)
    88  }
    89  
    90  func BytesToInt(curByte []byte) int {
    91  	bytesBuffer := bytes.NewBuffer(curByte)
    92  	var x int32
    93  	binary.Read(bytesBuffer, binary.BigEndian, &x)
    94  	b := int(x)
    95  	return b
    96  }
    97  
    98  func BytesToString(curByte []byte) string {
    99  	return string(curByte)
   100  }
   101  
   102  func IntToBytes(curInt int) []byte {
   103  	x := int32(curInt)
   104  	bytesBuffer := bytes.NewBuffer([]byte{})
   105  	binary.Write(bytesBuffer, binary.BigEndian, &x)
   106  	return bytesBuffer.Bytes()
   107  }
   108  
   109  func Uint64ToBytes(val uint64) []byte {
   110  	buf := make([]byte, 8)
   111  	binary.BigEndian.PutUint64(buf, val)
   112  	return buf[:]
   113  }
   114  
   115  func Uint32ToBytes(val uint32) []byte {
   116  	buf := make([]byte, 4)
   117  	binary.BigEndian.PutUint32(buf, val)
   118  	return buf[:]
   119  }
   120  
   121  func HexToAddress(b []byte) common.Address {
   122  	return common.HexToAddress(string(b))
   123  }
   124  
   125  func BytesTouint32(b []byte) uint32 {
   126  	b = append(make([]byte, 4-len(b)), b...)
   127  	return binary.BigEndian.Uint32(b)
   128  }
   129  
   130  func BytesTouint64(b []byte) uint64 {
   131  	b = append(make([]byte, 8-len(b)), b...)
   132  	return binary.BigEndian.Uint64(b)
   133  }