将字符串解析为特定类型的整数(int8、int16、int32、int64)。

huangapple go评论87阅读模式
英文:

Parse string to specific type of int (int8, int16, int32, int64)

问题

我正在尝试在Go中将字符串解析为整数。我在其中遇到的问题是在文档中提到的语法如下:

ParseInt(s string, base int, bitSize int)

其中,s 是要解析的字符串,base 由字符串的前缀隐含:对于 "0x",基数为 16;对于 "0",基数为 8;否则基数为 10。

我在 bitSize 参数上遇到了问题。根据 ParseInt 的文档,它指定了结果必须适应的整数类型。位大小 0、8、16、32 和 64 对应于 int、int8、int16、int32 和 int64。

但是对于所有的值,如 0、8、16、32 和 64,我得到的返回值类型都是相同的,即 int64 类型。

有人能指出我漏掉了什么吗?

代码:https://play.golang.org/p/F3LbUh_maY

英文:

I am trying to parse a string into an integer in Go. The Problem I found with it is in the documentation its mentioned syntax is as follows:

ParseInt(s string, base int, bitSize int)

where, s is the string to be parsed, base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

The bitSize parameter is where I am facing problem. As per documentation of ParseInt, it specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

But for all the values like 0, 8, 16, 32, and 64. I am getting same type return value. I.e of int64 type.

Could anyone point me out what am I missing.

Code: https://play.golang.org/p/F3LbUh_maY

答案1

得分: 49

根据文档

ParseInt函数始终返回int64类型的值。此外,

bitSize参数指定结果必须适应的整数类型。

因此,bitSize参数只是告诉你在解析字符串值之后,它应该适应的位大小。如果不适应,则会发生超出范围的情况。

就像在这个kbd中所示的例子:strconv.ParseInt("192", 10, 8)(你可以看到解析后的值将大于int8的最大值)。

如果你想将其解析为所需的任何值,只需在解析后使用int8(i)int8int16int32)即可。

P.S. 由于你提到了如何转换为特定的intX,我还要提一下,也可以转换为无符号整数

英文:

As per documentation

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

ParseInt always return int64 no matter what. Moreover

> The bitSize argument specifies the integer type that the result must
> fit into

So basically the your bitSize parameter only tells that the string value that you are going to parse should fit the bitSize after parsing. If not, out of range will happen.

Like in this <kbd>PlayGround</kbd>: strconv.ParseInt(&quot;192&quot;, 10, 8) (as you see the value after the parsing would be bigger than maximum value of int8).

If you want to parse it to whatever value you need, just use int8(i) afterwards (int8, int16, int32).

P.S. because you touched the topic how to convert to specific intX, I would outline that it is also possible to convert to unsigned int.

答案2

得分: 41

ParseInt始终返回一个int64,你需要将结果转换为你想要的类型。当你将32作为第三个参数传递时,如果解析的值无法适应int32,则会出现解析错误,但返回的类型始终是int64

例如:

i, err := strconv.ParseInt("9207", 10, 32)
if err != nil {
    panic(err)
}
result := int32(i)
fmt.Printf("Parsed int is %d\n", result)
英文:

ParseInt always returns an int64, and you need to convert the result to your desired type. When you pass 32 as the third argument, then you'll get a parse error if the parsed value won't fit into an int32, but the returned type is always int64.

For example:

i, err := strconv.ParseInt(&quot;9207&quot;, 10, 32)
if err != nil {
	panic(err)
}
result := int32(i)
fmt.Printf(&quot;Parsed int is %d\n&quot;, result)

答案3

得分: 2

你可以使用Sscan函数:

package main
import "fmt"

func main() {
   {
      var n int8
      fmt.Sscan("100", &n)
      fmt.Println(n == 100)
   }
   {
      var n int16
      fmt.Sscan("100", &n)
      fmt.Println(n == 100)
   }
   {
      var n int32
      fmt.Sscan("100", &n)
      fmt.Println(n == 100)
   }
   {
      var n int64
      fmt.Sscan("100", &n)
      fmt.Println(n == 100)
   }
}

https://golang.org/pkg/fmt#Sscan

英文:

You can use Sscan:

package main
import &quot;fmt&quot;

func main() {
   {
      var n int8
      fmt.Sscan(&quot;100&quot;, &amp;n)
      fmt.Println(n == 100)
   }
   {
      var n int16
      fmt.Sscan(&quot;100&quot;, &amp;n)
      fmt.Println(n == 100)
   }
   {
      var n int32
      fmt.Sscan(&quot;100&quot;, &amp;n)
      fmt.Println(n == 100)
   }
   {
      var n int64
      fmt.Sscan(&quot;100&quot;, &amp;n)
      fmt.Println(n == 100)
   }
}

https://golang.org/pkg/fmt#Sscan

huangapple
  • 本文由 发表于 2015年5月18日 17:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/30299649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定