'how Convert 0xffffffff to -1 in go [duplicate]

I have the following string, I want to convert it to a negative number in parentheses, can someone tell me how to do it in golang?

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff -> (-1)
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe -> (-2)

I know there are some similar looking answers here, but this is not what I want.



Solution 1:[1]

Based on the answer that you've linked you can do the following:

func main() {
    slice := []string{
        "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
    }

    for _, s := range slice {
        s = strings.TrimPrefix(s, "0x")

        i := &big.Int{}
        i, _ = i.SetString(s, 16)
        s = fmt.Sprintf("(%d)", i.Int64())

        fmt.Println(s)
    }
}

// outputs:
// (-1)
// (-2)

https://go.dev/play/p/l9WJ_KwsYu-

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1