10 examples of 'golang hex to int' in Go

Every line of 'golang hex to int' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
178func hexToInt(hexStr string) int {
179 result, _ := strconv.ParseUint(strings.Replace(hexStr, "0x", "", -1), 16, 64)
180 return int(result)
181}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

110func hex2int(hexStr string) uint64 {
111 // remove 0x suffix if found in the input string
112 cleaned := removeHexSuffix(hexStr)
113
114 // base 16 for hexadecimal
115 result, _ := strconv.ParseUint(cleaned, 16, 64)
116 return result
117}
243func hexRunesToInt(b []rune) int {
244 sum := 0
245 for i := 0; i < len(b); i++ {
246 sum += hexToInt(b[i]) << (uint(len(b)-i-1) * 4)
247 }
248 return sum
249}
187func parseHexUint(v []byte) (n uint64, err error) {
188 for _, b := range v {
189 n <<= 4
190 switch {
191 case '0' <= b && b <= '9':
192 b = b - '0'
193 case 'a' <= b && b <= 'f':
194 b = b - 'a' + 10
195 case 'A' <= b && b <= 'F':
196 b = b - 'A' + 10
197 default:
198 return 0, errors.New("invalid byte in chunk length")
199 }
200 n |= uint64(b)
201 }
202 return
203}
75func wire_intToUint(i int64) uint64 {
76 // signed arithmetic shift
77 return uint64((i >> 63) ^ (i << 1))
78}
921func hexToByte(lo, hi byte) (byte, bool) {
922 h, ok1 := fromHexChar(hi)
923 l, ok2 := fromHexChar(lo)
924 return h<<4 + l, ok1 && ok2
925}
130func HexToBigInt(t *testing.T, in string) *big.Int {
131 s := in[2:]
132 b, err := hex.DecodeString(s)
133 require.NoError(t, err)
134 return big.NewInt(0).SetBytes(b)
135}
61func Hex2Bin(s string) string {
62 bin, err := strconv.ParseInt(s, 16, 64)
63 if err != nil {
64 log.Fatal(err)
65 }
66 return strconv.FormatInt(bin, 2)
67}
65func uintToInt(x uint64) int64 {
66 i := int64(x >> 1)
67 if x&1 != 0 {
68 i = ^i
69 }
70 return i
71}
149func Int2HexStr(num int) (hex string) {
150 if num == 0 {
151 return "0"
152 }
153
154 for num > 0 {
155 r := num % 16
156
157 c := "?"
158 if r >= 0 && r <= 9 {
159 c = string(r + '0')
160 } else {
161 c = string(r + 'a' - 10)
162 }
163 hex = c + hex
164 num = num / 16
165 }
166 return hex
167}

Related snippets