# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5423927,"math_prob":0.9083179,"size":6991,"snap":"2023-14-2023-23","text_gpt3_token_len":1532,"char_repetition_ratio":0.17375125,"word_repetition_ratio":0.43178037,"special_character_ratio":0.23158346,"punctuation_ratio":0.07131012,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9716296,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-28T09:50:51Z\",\"WARC-Record-ID\":\"<urn:uuid:f87be562-ccb7-4588-af3d-b5f47f9baabb>\",\"Content-Length\":\"45527\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3a028d32-5362-410c-b189-66408089dee0>\",\"WARC-Concurrent-To\":\"<urn:uuid:f0c41137-f09b-41bd-83c7-12a99f2104c0>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/golang-program-to-convert-char-type-variables-to-int-using-inbuilt-functions\",\"WARC-Payload-Digest\":\"sha1:M6VKPKKGEBJNX5ABWGVKGOP6CD4WNHTC\",\"WARC-Block-Digest\":\"sha1:PG2VXKOIISO23TG5WYOU7CVHPRNENMC5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224643663.27_warc_CC-MAIN-20230528083025-20230528113025-00422.warc.gz\"}"}