go - ParseInt() 中使用的基本参数是什么?

标签 go

每当我们尝试将字符串转换为我们使用的 int 时,我都遇到过许多示例代码:

parseValue, err := strconv.ParseInt(value, 10, 64)

所以上面的代码 ParseInt() 有三个参数。来自文档代码:

func ParseInt(s string, base int, bitSize int) (i int64, err error) {

我试图理解这里的 base int,所以我将值从 0 更改为 16。PlayGolang .

当输入为010 时结果正常。 010 以外的数字处于 panic 状态。我还是一头雾水,不明白。有人可以解释一下 baseParseInt() 中的用途吗?

最佳答案

您正在将字符串转换为整数。该方法要求您提供字符串所在的基数(数字系统)。您可以在此处了解有关数字系统的更多信息:https://en.wikipedia.org/wiki/Radix

例如(使用您提供的代码片段 here ):

var s string = "1111" // This string is in binary (Base 2)
i, err := strconv.ParseInt(s, 2, 64) // Give the base as 2

结果:

Hello, 15 with type int64!

关于go - ParseInt() 中使用的基本参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499643/

相关文章:

string - 在 Golang 中生成固定长度的随机十六进制字符串的有效方法?

json - 使用反射在 Go 中创建 map

go - 使用/cmd 结构构建 Go 模块时出错

Golang 中的多线程套接字连接

go - fasthttp + fasthttprouter,尝试写中间件

amazon-web-services - 获取自日期以来创建的EC2实例列表

go - Cgo:抱歉,未实现:64 位模式未编译

go - 如果给定两个像素值,例如 :- pt1(0, 0) 和 pt2(480, 240),我如何在 jpeg 上绘制一个填充的矩形框?

来自十六进制的 Node.js 缓冲区和 Golang 中的 readUInt16BE

Golang https 服务器以字节数组形式传递 certFile 和 kyeFile