github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/bytes.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450073048125440>
    11  
    12  
    13  //package common包含各种助手函数。
    14  package common
    15  
    16  import "encoding/hex"
    17  
    18  //tohex返回b的十六进制表示形式,前缀为“0x”。
    19  //对于空切片,返回值为“0x0”。
    20  //
    21  //已弃用:请改用hexutil.encode。
    22  func ToHex(b []byte) string {
    23  	hex := Bytes2Hex(b)
    24  	if len(hex) == 0 {
    25  		hex = "0"
    26  	}
    27  	return "0x" + hex
    28  }
    29  
    30  //ToHexarray基于[]字节创建十六进制字符串数组
    31  func ToHexArray(b [][]byte) []string {
    32  	r := make([]string, len(b))
    33  	for i := range b {
    34  		r[i] = ToHex(b[i])
    35  	}
    36  	return r
    37  }
    38  
    39  //FromHex返回十六进制字符串s表示的字节。
    40  //s的前缀可以是“0x”。
    41  func FromHex(s string) []byte {
    42  	if len(s) > 1 {
    43  		if s[0:2] == "0x" || s[0:2] == "0X" {
    44  			s = s[2:]
    45  		}
    46  	}
    47  	if len(s)%2 == 1 {
    48  		s = "0" + s
    49  	}
    50  	return Hex2Bytes(s)
    51  }
    52  
    53  //CopyBytes返回所提供字节的精确副本。
    54  func CopyBytes(b []byte) (copiedBytes []byte) {
    55  	if b == nil {
    56  		return nil
    57  	}
    58  	copiedBytes = make([]byte, len(b))
    59  	copy(copiedBytes, b)
    60  
    61  	return
    62  }
    63  
    64  //hashexprefix验证str以“0x”或“0x”开头。
    65  func hasHexPrefix(str string) bool {
    66  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    67  }
    68  
    69  //IShexCharacter返回C的bool作为有效的十六进制。
    70  func isHexCharacter(c byte) bool {
    71  	return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
    72  }
    73  
    74  //ISHEX验证每个字节是否为有效的十六进制字符串。
    75  func isHex(str string) bool {
    76  	if len(str)%2 != 0 {
    77  		return false
    78  	}
    79  	for _, c := range []byte(str) {
    80  		if !isHexCharacter(c) {
    81  			return false
    82  		}
    83  	}
    84  	return true
    85  }
    86  
    87  //bytes2hex返回d的十六进制编码。
    88  func Bytes2Hex(d []byte) string {
    89  	return hex.EncodeToString(d)
    90  }
    91  
    92  //hex2bytes返回十六进制字符串str表示的字节。
    93  func Hex2Bytes(str string) []byte {
    94  	h, _ := hex.DecodeString(str)
    95  	return h
    96  }
    97  
    98  //hex2bytesfixed返回指定固定长度flen的字节。
    99  func Hex2BytesFixed(str string, flen int) []byte {
   100  	h, _ := hex.DecodeString(str)
   101  	if len(h) == flen {
   102  		return h
   103  	}
   104  	if len(h) > flen {
   105  		return h[len(h)-flen:]
   106  	}
   107  	hh := make([]byte, flen)
   108  	copy(hh[flen-len(h):flen], h)
   109  	return hh
   110  }
   111  
   112  //rightpaddbytes零个焊盘向右切片至长度l。
   113  func RightPadBytes(slice []byte, l int) []byte {
   114  	if l <= len(slice) {
   115  		return slice
   116  	}
   117  
   118  	padded := make([]byte, l)
   119  	copy(padded, slice)
   120  
   121  	return padded
   122  }
   123  
   124  //LeftPadBytes将零个焊盘向左切片至长度L。
   125  func LeftPadBytes(slice []byte, l int) []byte {
   126  	if l <= len(slice) {
   127  		return slice
   128  	}
   129  
   130  	padded := make([]byte, l)
   131  	copy(padded[l-len(slice):], slice)
   132  
   133  	return padded
   134  }
   135