github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/common/bytes.go (about)

     1  // Copyright 2014 The elementalcore Authors
     2  // This file is part of the elementalcore library.
     3  //
     4  // The elementalcore 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 elementalcore 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 elementalcore library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package common contains various helper functions.
    18  package common
    19  
    20  import (
    21  	"encoding/hex"
    22  )
    23  
    24  func ToHex(b []byte) string {
    25  	hex := Bytes2Hex(b)
    26  	// Prefer output of "0x0" instead of "0x"
    27  	if len(hex) == 0 {
    28  		hex = "0"
    29  	}
    30  	return "0x" + hex
    31  }
    32  
    33  func FromHex(s string) []byte {
    34  	if len(s) > 1 {
    35  		if s[0:2] == "0x" || s[0:2] == "0X" {
    36  			s = s[2:]
    37  		}
    38  		if len(s)%2 == 1 {
    39  			s = "0" + s
    40  		}
    41  		return Hex2Bytes(s)
    42  	}
    43  	return nil
    44  }
    45  
    46  // Copy bytes
    47  //
    48  // Returns an exact copy of the provided bytes
    49  func CopyBytes(b []byte) (copiedBytes []byte) {
    50  	if b == nil {
    51  		return nil
    52  	}
    53  	copiedBytes = make([]byte, len(b))
    54  	copy(copiedBytes, b)
    55  
    56  	return
    57  }
    58  
    59  func HasHexPrefix(str string) bool {
    60  	l := len(str)
    61  	return l >= 2 && str[0:2] == "0x"
    62  }
    63  
    64  func IsHex(str string) bool {
    65  	l := len(str)
    66  	return l >= 4 && l%2 == 0 && str[0:2] == "0x"
    67  }
    68  
    69  func Bytes2Hex(d []byte) string {
    70  	return hex.EncodeToString(d)
    71  }
    72  
    73  func Hex2Bytes(str string) []byte {
    74  	h, _ := hex.DecodeString(str)
    75  
    76  	return h
    77  }
    78  
    79  func Hex2BytesFixed(str string, flen int) []byte {
    80  	h, _ := hex.DecodeString(str)
    81  	if len(h) == flen {
    82  		return h
    83  	} else {
    84  		if len(h) > flen {
    85  			return h[len(h)-flen:]
    86  		} else {
    87  			hh := make([]byte, flen)
    88  			copy(hh[flen-len(h):flen], h[:])
    89  			return hh
    90  		}
    91  	}
    92  }
    93  
    94  func RightPadBytes(slice []byte, l int) []byte {
    95  	if l <= len(slice) {
    96  		return slice
    97  	}
    98  
    99  	padded := make([]byte, l)
   100  	copy(padded, slice)
   101  
   102  	return padded
   103  }
   104  
   105  func LeftPadBytes(slice []byte, l int) []byte {
   106  	if l <= len(slice) {
   107  		return slice
   108  	}
   109  
   110  	padded := make([]byte, l)
   111  	copy(padded[l-len(slice):], slice)
   112  
   113  	return padded
   114  }