github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/common/bytes.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package common contains various helper functions.
    18  package common
    19  
    20  import "encoding/hex"
    21  
    22  func ToHex(b []byte) string {
    23  	hex := Bytes2Hex(b)
    24  	// Prefer output of "0x0" instead of "0x"
    25  	if len(hex) == 0 {
    26  		hex = "0"
    27  	}
    28  	return "0x" + hex
    29  }
    30  
    31  func FromHex(s string) []byte {
    32  	if len(s) > 1 {
    33  		if s[0:2] == "0x" {
    34  			s = s[2:]
    35  		}
    36  		if len(s)%2 == 1 {
    37  			s = "0" + s
    38  		}
    39  		return Hex2Bytes(s)
    40  	}
    41  	return nil
    42  }
    43  
    44  // Copy bytes
    45  //
    46  // Returns an exact copy of the provided bytes
    47  func CopyBytes(b []byte) (copiedBytes []byte) {
    48  	copiedBytes = make([]byte, len(b))
    49  	copy(copiedBytes, b)
    50  
    51  	return
    52  }
    53  
    54  func IsHex(str string) bool {
    55  	l := len(str)
    56  	return l >= 4 && l%2 == 0 && str[0:2] == "0x"
    57  }
    58  
    59  func Bytes2Hex(d []byte) string {
    60  	return hex.EncodeToString(d)
    61  }
    62  
    63  func Hex2Bytes(str string) []byte {
    64  	h, _ := hex.DecodeString(str)
    65  
    66  	return h
    67  }
    68  
    69  func Hex2BytesFixed(str string, flen int) []byte {
    70  	h, _ := hex.DecodeString(str)
    71  	if len(h) == flen {
    72  		return h
    73  	} else {
    74  		if len(h) > flen {
    75  			return h[len(h)-flen:]
    76  		} else {
    77  			hh := make([]byte, flen)
    78  			copy(hh[flen-len(h):flen], h[:])
    79  			return hh
    80  		}
    81  	}
    82  }
    83  
    84  func RightPadBytes(slice []byte, l int) []byte {
    85  	if l < len(slice) {
    86  		return slice
    87  	}
    88  
    89  	padded := make([]byte, l)
    90  	copy(padded[0:len(slice)], slice)
    91  
    92  	return padded
    93  }
    94  
    95  func LeftPadBytes(slice []byte, l int) []byte {
    96  	if l < len(slice) {
    97  		return slice
    98  	}
    99  
   100  	padded := make([]byte, l)
   101  	copy(padded[l-len(slice):], slice)
   102  
   103  	return padded
   104  }