aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/onrik/ethrpc/helpers.go
blob: e98030055281bc4aa0a7676f35674b8fe55076db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ethrpc

import (
    "fmt"
    "math/big"
    "strconv"
    "strings"
)

// ParseInt parse hex string value to int
func ParseInt(value string) (int, error) {
    i, err := strconv.ParseInt(strings.TrimPrefix(value, "0x"), 16, 64)
    if err != nil {
        return 0, err
    }

    return int(i), nil
}

// ParseBigInt parse hex string value to big.Int
func ParseBigInt(value string) (big.Int, error) {
    i := big.Int{}
    _, err := fmt.Sscan(value, &i)

    return i, err
}

// IntToHex convert int to hexadecimal representation
func IntToHex(i int) string {
    return fmt.Sprintf("0x%x", i)
}

// BigToHex covert big.Int to hexadecimal representation
func BigToHex(bigInt big.Int) string {
    if bigInt.BitLen() == 0 {
        return "0x0"
    }

    return "0x" + strings.TrimPrefix(fmt.Sprintf("%x", bigInt.Bytes()), "0")
}