Cast long string to int64 - Go

1 view
Skip to first unread message

Ook via StackOverflow

unread,
Dec 1, 2013, 11:49:03 PM12/1/13
to google-appengin...@googlegroups.com

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?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/20321022/cast-long-string-to-int64-go

peterSO via StackOverflow

unread,
Dec 2, 2013, 12:04:03 AM12/2/13
to google-appengin...@googlegroups.com

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


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/20321022/cast-long-string-to-int64-go/20321170#20321170
Reply all
Reply to author
Forward
0 new messages