ADVERTISEMENT
ADVERTISEMENT

Golang strconv.ParseInt() Function with Examples

Golang | strconv.ParseInt() Function: Here, we are going to learn about the ParseInt() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 10, 2021

strconv.ParseInt()

The ParseInt() function is an inbuilt function of the strconv package which is used to convert the given string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding integer value i. The given string may begin with a leading sign: "+" or "-" to convert as a positive or negative integer.

It accepts three parameters (s, base, bitSize)) and returns the integer number converted from the given string.

Syntax:

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

Parameter(s):

  • s : String value which is to be parsed in the integer number.
  • base : The base value of the given value, it can be 0, 2 to 36.
  • bitSize : It defines the integer type,
    • 0 for int
    • 8 for int8
    • 16 for int16
    • 32 for int32
    • 64 for int64

Return Value:

The return type of the ParseInt() function is (int64, error), it returns the integer number converted from the given string.

Example 1:

// Golang program to demonstrate the
// example of strconv.ParseInt() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseInt("65535", 10, 32))
	fmt.Println(strconv.ParseInt("+65535", 10, 32))
	fmt.Println(strconv.ParseInt("-65535", 10, 32))
	fmt.Println()

	fmt.Println(strconv.ParseInt("101010", 2, 32))
	fmt.Println(strconv.ParseInt("111111", 2, 32))
	fmt.Println(strconv.ParseInt("00001111000", 2, 32))
	fmt.Println(strconv.ParseInt("111111111111111111111111111111110011", 2, 64))
	fmt.Println()

	fmt.Println(strconv.ParseInt("65535", 16, 32))
	fmt.Println(strconv.ParseInt("12fccc", 16, 32))
	fmt.Println(strconv.ParseInt("fafafafa", 16, 64))
	fmt.Println(strconv.ParseInt("ffffffff", 16, 64))
}

Output:

65535 <nil>
65535 <nil>
-65535 <nil>

42 <nil>
63 <nil>
120 <nil>
68719476723 <nil>

415029 <nil>
1244364 <nil>
4210752250 <nil>
4294967295 <nil>

Example 2:

// Golang program to demonstrate the
// example of strconv.ParseInt() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := "12345678"

	s, err := strconv.ParseInt(v, 10, 32)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "-123456789098765"
	s, err = strconv.ParseInt(v, 10, 64)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "1234567890987654321"
	s, err = strconv.ParseInt(v, 10, 64)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "1111111111111111111111000000000111111"
	s, err = strconv.ParseInt(v, 2, 64)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
	fmt.Println()

	v = "ffffffffff"
	s, err = strconv.ParseInt(v, 16, 32)
	if err == nil {
		fmt.Println("Parsing done...")
		fmt.Printf("%T, %v\n", s, s)
	} else {
		fmt.Println("Parsing failed...")
		fmt.Printf("Error:%v\n", err)
	}
}

Output:

Parsing done...
int64, 12345678

Parsing done...
int64, -123456789098765

Parsing done...
int64, 1234567890987654321

Parsing done...
int64, 137438920767

Parsing failed...
Error:strconv.ParseInt: parsing "ffffffffff": value out of range

Golang strconv Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.