Tuesday, November 20, 2018

Golang Example - strconv ParseInt Function explained

In this blog post, We are going to learn strconv ParseInt Function with examples in the Go Language.
Golang Example - strconv ParseInt Function explained

strconv ParseInt Function

strconv package provides functions for converting String to or from different data types.
PaseInt() is one of the functions used to convert String to Integer types and used for Number conversions. You can get official documentation from here
Syntax 
Here is a signature of ParseInt example
func ParseInt(s string, radix/base int, bitSize int) (i int64, err error)
Arguments List -
Arguments Description
string radix or base is a value from 2 to 36
int bitSize reduced the output converted the int value
Returned data -
int64 value is returned error is returned

Function Usage

parseInt() function accepts and parse string type and convert based on the base value, returned an int type value.
if base or radix argument is zero, Go compiler assumes the followings things
  • if a string starts with "0x", base assumes as 16 ie hexadecimal
  • if a string starts with "0", base assumes as 8 ie octal number
  • if a string starts with any other value, base assumes as 10 ie decimal number
if the string contains any space before or after, It is invalid and gives runtime error - strconv.ParseInt: parsing " 243 ": invalid syntax
if bitSize is given lesser than the size of the converted int value, It gives runtime error - strconv.ParseInt: parsing "5612": value out of range int64

ParseInt Example

The below example explains String conversion to Int64
  • String to Int64 conversion using parseInt 
  • String Hexadecimal to Int64 conversion using parseInt 
  • String Octal to Int64 conversion  
package main

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

func main() {
 // bitSize argument example usage
 fmt.Println(" ======================= bitSize argument example ======================= ")
 v1, err := strconv.ParseInt("5612", 0, 2)
 fmt.Println(v1, err, reflect.TypeOf(v1))
 v2, err := strconv.ParseInt("5612", 0, 8)
 fmt.Println(v2, err, reflect.TypeOf(v2))
 v3, err := strconv.ParseInt("5612", 0, 16)
 fmt.Println(v3, err, reflect.TypeOf(v3))
 // base argument example usage
 fmt.Println(" ======================= base argument example ======================= ")
 v4, err := strconv.ParseInt("5612", 0, 16)
 fmt.Println(v4, err, reflect.TypeOf(v4))
 v5, err := strconv.ParseInt("5612", 8, 32)
 fmt.Println(v5, err, reflect.TypeOf(v5))
 v6, err := strconv.ParseInt("5612", 16, 64)
 fmt.Println(v6, err, reflect.TypeOf(v6))
 // String Argument argument example usage
 fmt.Println(" ======================= String argument example ======================= ")
 v7, err := strconv.ParseInt("0x123", 0, 64) // hexdecimal Numeric Conversion
 fmt.Println(v7, err, reflect.TypeOf(v7))
 v8, err := strconv.ParseInt("0123", 0, 64) // Octal Number Conversion
 fmt.Println(v8, err, reflect.TypeOf(v8))
 v9, err := strconv.ParseInt("123", 0, 64) // any other number
 fmt.Println(v9, err, reflect.TypeOf(v9))
 // Invalid String argument conversion example usage
 fmt.Println(" ======================= Invalid String argument conversion example usage ======================= ")
 v10, err := strconv.ParseInt("abc", 0, 64) // any other number
 fmt.Println(v9, err, reflect.TypeOf(v10))
}
When you compiled and executed the program, It gives output as follows
 ======================= bitSize argument example =======================
1 strconv.ParseInt: parsing "5612": value out of range int64
127 strconv.ParseInt: parsing "5612": value out of range int64
5612 <nil> int64
 ======================= base argument example =======================
5612 <nil> int64
2954 <nil> int64
22034 <nil> int64
 ======================= String argument example =======================
291 <nil> int64
83 <nil> int64
123 <nil> int64
 ======================= Invalid String argument conversion example usage =======================
123 strconv.ParseInt: parsing "abc": invalid syntax int64

Convert String to integer types - Int8,Int16,Int 32

parseInt always return Int64 value.With Int8(int64 value) code, returned Int64 value is converted to Int8 type. Likewise, Int16(),Int32() are used to converted to Int16, Int32 types.
Here is an example program to convert to other types
package main

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

func main() {
 v1, err := strconv.ParseInt("1", 0, 16)
 fmt.Println(v1, err, reflect.TypeOf(v1))
 var v2 int8
 var v3 int16
 var v4 int32
 v2 = int8(v1) // Convert to int8
 fmt.Println(v2, err, reflect.TypeOf(v2))
 v3 = int16(v1) // Convert to int16
 fmt.Println(v3, err, reflect.TypeOf(v3))
 v4 = int32(v1) // Convert to int32
 fmt.Println(v4, err, reflect.TypeOf(v4))
}
Output is
1 <nil> int64
1 <nil> int8
1 <nil> int16
1 <nil> int32

Related article


EmoticonEmoticon