github.com/wormhole-foundation/wormhole-explorer/common@v0.0.0-20240604151348-09585b5b97c5/utils/hex.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/hex"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  func StartsWith0x(input string) bool {
    10  	return strings.HasPrefix(input, "0x") || strings.HasPrefix(input, "0X")
    11  }
    12  
    13  func Remove0x(input string) string {
    14  	return strings.Replace(input, "0x", "", -1)
    15  }
    16  
    17  func DecodeUint64(input string) (uint64, error) {
    18  	input = Remove0x(input)
    19  	return strconv.ParseUint(input, 16, 64)
    20  }
    21  
    22  func EncodeHex(v uint64) string {
    23  	return "0x" + strconv.FormatUint(v, 16)
    24  }
    25  
    26  func NormalizeHex(s string) string {
    27  	lower := strings.ToLower(s)
    28  	return Remove0x(lower)
    29  }
    30  
    31  func NormalizeBytesToHex(b []byte) string {
    32  	return NormalizeHex(hex.EncodeToString(b))
    33  }