AppDividend
Latest Code Tutorials

Golang: How To Convert String To Int64 In Go

0

Golang strconv.ParseInt() is an inbuilt function that parses a decimal string (base 10) and checks if it fits into an int64. The size of an int is implementation-specific, it’s either 32 or 64 bits, and that is why you won’t lose any key info when converting from int to  int64.

Golang string to int64

Go strconv.ParseInt to convert a decimal string (base 10) and check that it fits into a 64-bit signed integer.

func ParseInt()

ParseInt interprets the string in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i. If a base argument is 0, the true base is implied by a string’s prefix: 2 for “0b”, 8 for “0” or “0o”, 16 for “0x”, and 10 otherwise.

Also, for argument base 0 only, underscore characters are permitted as defined by the Golang syntax for integer literals.

The bitSize parameter specifies an integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. If the bitSize is below 0 or above 64, an error is returned.

Let’s use the Golang strconv.ParseInt() function to convert string to int64 in Golang.

// hello.go

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str := "101"
	n, err := strconv.ParseInt(str, 10, 64)
	if err == nil {
		fmt.Printf("%d of type %T", n, n)
	}
}

Output

go run hello.go
101 of type int64

The two numeric arguments represent the base (0, 2 to 36) and a bit size (0 to 64).

If the first argument is 0, the base is implied by a string’s prefix: base 16 for “0x”, base 8 for “0”, and base 10 otherwise.

The second argument describes an integer type that a result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

Example 2: Convert Golang string to int

See the following code.

// hello.go

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v32 := "-354634382"
	if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

	v64 := "-3546343826724305832"
	if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
}

Output

go run hello.go
int64, -354634382
int64, -3546343826724305832

Parsing string into int64

See the following code.

// hello.go

package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
	var s string = "112119462926854775"
	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Bonjour, %v with type %s!\n", i, reflect.TypeOf(i))
}

Output

go run hello.go
Bonjour, 112119462926854775 with type int64!

Conclusion

If you want to convert Golang string to int64, you should also pass in the 64 as the last argument to ParseInt(), or it might not produce the expected value on the 32-bit system.

See also

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.