Golang Program to Convert Char Type Variables to Int Using Inbuilt Functions


In this tutorial, we will learn how to convert character type to integer type variable using Golang programming language.

In the Go programming language, there is no char data type. It uses bytes and rune and strings to represent character values.

Example-1: Golang Program Code to Convert Char to Int Using Atoi() Function

Syntax

func Atoi(str string) (int, error)
Here, str is the string.

Atoi() function is equivalent to ParseInt(str string, base int, bitSize int) is used to convert string type into int type and to access Atoi() function you need to import strconv Package in your program.

Algorithm

  • STEP 1 − Import the package fmt and strconv package.

  • STEP 2 − Start function main ().

  • STEP 3 − Declare and initialize the character.

  • STEP 4 − Calling the built-in function strconv.Atoi().

  • STEP 5 − Print the integer using fmt.Printf().

Example

package main

// fmt package provides the function to print anything
// Package strconv implements conversions to and from
// string representations of basic data types

import (
   "fmt"
   "strconv"
)
// start the function main
func main() {
fmt.Println("GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES")
   // initializing the string variable
   str := "136"
   // calling the built-in function strconv.Atoi()
   // this function converts string type into int type
   y, e := strconv.Atoi(str)
   if e == nil {
      // print the integer type variable
      fmt.Printf("%T \n %v", y, y)
   }
}

Output

GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES
int 
136

Description of The Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt. We also imported Package strconv implements conversions to and from string representations of basic data types.

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Next declare and initialize the character to be converted to integer type.

  • Next, we call the built-in function strconv.Atoi (). If the execution is successful, the atoi () method returns the converted integer value.

  • Finally Print the converted char to integer using fmt.Printf () which formats according to a format specifier and writes to standard output.

Example-2: Golang Program Code to Convert Char to Int Using Typecasting

Algorithm

  • STEP 1 − Import the package fmt

  • STEP 2 − Start function main ()

  • STEP 3 − Declare and initialize the char type variables.

  • STEP 4 − ASCII value of characters is assigned

  • Step 5 − Assign the char variables to num1 and num2.

  • STEP 6 − Print the integer using fmt.Println()

Example

package main
// fmt package provides the function to print anything
import "fmt"

// start the function main ()
func main(){
fmt.Println("GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES")
   // create char variables
   var  a  byte = '8';
   var  b  byte = 'g';
   // convert char variables to int
   // ASCII value of characters is assigned
   num1 := a;
   num2 := b;
   // print the integer values
   fmt.Println(num1)
   fmt.Println(num2)
}

Output

GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES
56 
103

Description

  • In the above program example, we first declare the package main

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything

  • Next declare and initialize the char type variables to be converted to integer type. The char variables are a and b

  • Note that we have assigned the char variables to int variables. Here, instead of the characters, the ASCII value of the characters are assigned to the integer variables

  • Next assign the char type to num1 and num2

  • Finally Print the integer using fmt.Println() which formats according to a format specifier and writes to standard output. We get the value 56 (ASCII value of '8') and 103 (ASCII value of 'g') as output.

Example-3: Char to an Integer by Subtracting With '0'

Algorithm

  • STEP 1 − Import the package fmt.

  • STEP 2 − Start function main ().

  • STEP 3 − Declare and initialize the char type variables.

  • STEP 4 − Convert char variables to int by subtracting with char 0.

  • Step 5 − Assign the char variables to num1 and num2.

  • STEP 6 − Print the integer using fmt.Println().

Example

// GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INT

package main
// fmt package provides the function to print anything
import "fmt"

// start the function main
func main(){
fmt.Println("GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES")   
   // create char variables
   var  a  byte = '9';
   var  b  byte = '3';
   // convert char variables to int
   // by subtracting with char 0
   num1 := a - '0';
   num2 := b - '0';
   // print numeric value
   fmt.Println(num1)

   fmt.Println(num2)
    
}

Output

GOLANG PROGRAM TO CONVERT CHAR TYPE VARIABLES TO INTEGER TYPE VARIABLES
9
3

Description

  • In the above program example, we first declare the package’s main

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything

  • Next declare and initialize the char type variables to be converted to the integer type. The char variables are a and b

  • Next convert char variables to int by subtracting with char 0

  • Next assign the char type to num1 and num2

  • Finally Print the integer using fmt.Println() which formats according to a format specifier and writes to standard output.

Updated on: 29-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements