python - Golang 与 Python - 十六进制字符串到 Int

标签 python go type-conversion hex

我有一个十六进制字符串:

n = "0xd458985bc81e284609dd69267c73b8464e1795d5b91ce6ed8871ecbc5b6ec4d1"  

我可以使用以下方法在 python 中转换为 int:

mynum = int(n,16)

我得到了长号:96046857981227695367604088053507399752198003710848334588478940192231467697361

现在我将如何在 Golang 中执行此操作?

最佳答案

这是一个很好的问题(尽管与 Flimzy 发现的另一个问题相似)。主要问题是内置整数在 Go 中最多只能达到 64 位,但您可以使用 math/big 包。

fmt.Println(new(big.Int).SetString("0xd458985bc81e284609dd69267c73b8464e1795d5b91ce6ed8871ecbc5b6ec4d1", 0))

如果您没有前导 0x,则使用 16 作为基数:

fmt.Println(new(big.Int).SetString("d458985bc81e284609dd69267c73b8464e1795d5b91ce6ed8871ecbc5b6ec4d1", 16))

关于python - Golang 与 Python - 十六进制字符串到 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908139/

相关文章:

macos - 升级到 Mac OS 10.9.3 后 Bash 无法识别 go

去获取 hashcorp/levant 失败

string - 如何将 'string pointer' 转换为 Golang 中的字符串?

python - Go 中的 gRPC 服务器与 Python 中的客户端之间的兼容性

date - 如何计算结束日期

c++ - 将 DBL_MAX 转换为 int 的结果不同于 std::numeric_limits<double>::max() 到 int

ios - 在 Swift 中将 Int 转换为 String

python - 如何获得每个半年日期的 2 年期限

python - 根据元组的第一个条目从列表中删除重复的元组

Python网页爬取BeautifulSoup : getting both text and links