ADVERTISEMENT
ADVERTISEMENT

Golang strconv.ParseUint() Function with Examples

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

strconv.ParseUint()

The ParseUint() 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 unsigned integer value i. The ParseUint() function is similar to ParseInt() but for unsigned integers only, the sign (+ or -) is not allowed.

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

Syntax:

func ParseUint(s string, base int, bitSize int) (uint64, error)

Parameter(s):

  • s : String value which is to be parsed in the unsigned 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 uint
    • 8 for uint8
    • 16 for uint16
    • 32 for uint32
    • 64 for uint64

Return Value:

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

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseUint("65535", 10, 32))
	fmt.Println(strconv.ParseUint("123457", 10, 32))
	fmt.Println(strconv.ParseUint("108", 10, 32))
	fmt.Println()

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

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

Output:

65535 <nil>
123457 <nil>
108 <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.ParseUint() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := "12345678"

	s, err := strconv.ParseUint(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.ParseUint(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.ParseUint(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.ParseUint(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.ParseUint(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...
uint64, 12345678

Parsing done...
uint64, 123456789098765

Parsing done...
uint64, 1234567890987654321

Parsing done...
uint64, 137438920767

Parsing failed...
Error:strconv.ParseUint: 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.