将长字符串转换为int64 – Go

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

Cast long string to int64 - Go

问题

我正在寻找一种方法,在Go语言中将一个长字符串格式的整数转换为int64类型。我使用了strconv.Atoi,但是它给出了一个"值超出范围"的错误。我搜索到的答案是在strconv包中使用了ParseInt(s string, base int, bitSize int) (i int64, err error)函数。然而,我不明白在这个函数的参数中应该提供什么值,包括base和bitSize。

我正在尝试将我从HTTP请求中接收到的以字符串格式表示的datastore.Key.IntID()解析为int64类型,以便在Datastore上执行查询时创建新的键。

有人能解释一下base和bitSize参数以及在这种情况下应该提供什么值吗?

英文:

I am finding the way to cast an integer number in a long string format to int64 in Go. I used strconv.Atoi but it gave me an error said "value out of range". I searched for the answer I found

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

in strconv package. However, I don't understand what value should I provide for the function arguments including base and bitSize.

I am trying to parse datastore.Key.IntID() in string format that I received from HTTP request back to int64 for creating new keys to perform query on Datastore.

Could anyone explain me a bit about the base and bitSize arguments and what values should I provide in the arguments in this case?

答案1

得分: 10

// [func ParseInt][1]
// 
//     func ParseInt(s string, base int, bitSize int) (i int64, err error)
// 
// ParseInt将给定进制(2到36)中的字符串s解析为相应的值i,并返回。如果base == 0,则基数由字符串的前缀隐含:对于“0x”,基数为16;对于“0”,基数为8;否则,基数为10。
// 
// bitSize参数指定结果必须适应的整数类型。位大小0、8、16、32和64对应于int、int8、int16、int32和int64。
// 
// ParseInt返回的错误具有具体类型*NumError,并包含err.Num = s。如果s为空或包含无效的数字,则err.Err = ErrSyntax;如果与s对应的值无法由给定大小的有符号整数表示,则err.Err = ErrRange。

例如

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "9223372036854775807"
	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(i)
}

输出

9223372036854775807

[1]: http://golang.org/pkg/strconv/#ParseInt
英文:

> func ParseInt
>
> func ParseInt(s string, base int, bitSize int) (i int64, err error)
>
> ParseInt interprets a string s in the given base (2 to 36) and returns
> the corresponding value i. If base == 0, the base is implied by the
> string's prefix: base 16 for "0x", base 8 for "0", and base 10
> otherwise.
>
> The bitSize argument 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.
>
> The errors that ParseInt returns have concrete type *NumError and
> include err.Num = s. If s is empty or contains invalid digits, err.Err
> = ErrSyntax; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

For example,

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "9223372036854775807"
	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(i)
}

Output:

9223372036854775807

huangapple
  • 本文由 发表于 2013年12月2日 12:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/20321022.html
匿名

发表评论

匿名网友

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

确定