# Golang Program to convert Hexadecimal to Decimal

In this article we are going to understand how to covert Hexadecimal to Decimal number using Golang Program.

## Hexadecimal Number Vs Decimal Numbers

Any number contains 16 digits in it, it is based on base 16, which means hexadecimal numbers contains decimal number from 0 to 9 and extra 6 Alphabets. A−F.

Decimal numbers are the numbers which have base 10, which means each digit of a number can have an integer value ranging from 0 to depending on its position. For example, 23 is a decimal number.

## Example 1: Go Language Code to Convert Hexadecimal Number to Decimal using IN-BUILD Function

### Syntax

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


The ParseInt() function is used to convert a number from any other base value to integer number. It uses three arguments one is the base value that we wish to convert in string another is the base value to which we wish to convert to and the third one is the bit size of the result that we wish to obtain.

The function returns the result in a unsigned 64-bit number and an error message if any error gets generated in the conversion process.

### Algorithm

• Step 1 − import the package fmt, strconv, and strings.

• Step 2 − Start the main () function.

• Step 3 − Initialize the hexadecimal numbers to convert to decimal

• Step 4 − Pass the hexadecimal value through strconv.ParseInt() function.

• Step 5 − Finally print the decimal integer using fmt package.

### Example

package main
//import the required packages
import (
"fmt"
"strconv"
)

// calling the main function
func main() {

// use the parseInt() function to convert
decimal_num, err := strconv.ParseInt(hexadecimal_num, 16, 64)

// in case of any error
if err != nil {
panic(err)
}

// print the decimal number
fmt.Println("The decimal conversion of", hexadecimal_num, "is ", decimal_num)

//use the parseInt() function to convert
decimal_num, err = strconv.ParseInt(hexadecimal_num, 16, 64)

//in case of any error
if err != nil {
panic(err)
}

// print the decimal number
fmt.Println("The decimal conversion of", hexadecimal_num, "is ", decimal_num)

// use the parseInt() function to convert hexadecimal to decimal
decimal_num, err = strconv.ParseInt(hexadecimal_num, 16, 64)

//in case of any error
if err != nil {
panic(err)
}

// print the decimal number
fmt.Println("The decimal conversion of", hexadecimal_num, "is", decimal_num)
}


### Output

The decimal conversion of 23a is 570
The decimal conversion of 25f is 607
The decimal conversion of ffff is 65535


### Description of the Code

• First we have to import fmt package and the strconv package. fmt package allows us to print anything on the screen whereas strconv method contains the functions which we can use to convert a hexadecimal number to decimal.

• Then we need to initialize a string to store the hexadecimal number.

• In this example we will be using strconv.ParseInt() function.

• Pass the hexadecimal string that you wish to convert as the first argument to the function and the base value as the second argument to the function.

• Store the result in a separate variable. In this example we have used decimal_num variable to store the result.

• Then we can print the result on the screen using fmt.Println() function.

• Use different hexadecimal numbers and print the decimal numbers on the screen.

## Example 2: Go Lang Program to Convert Hexadecimal to Decimal using ParseUnit()

### Syntax

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


The ParseUnit() function is used to convert hexadecimal number to integer number. It uses three arguments one is the hexadecimal string that we wish to convert another is the base value to which we wish to convert hexadecimal number to and the third one is the integer type to which we wish to obtain the result like int.

The function returns the result in a unsigned 64-bit number and an error message if any error gets generated in the conversion process.

### Algorithm

• Step 1 − import the package fmt, strconv, and strings.

• Step 2 − Start the main () function.

• Step 3 − Initialize the hexadecimal number to convert to decimal

• Step 4 − Pass the hexadecimal value to strconv.ParseUint() function as an argument.

• Step 5 − Finally print the decimal integer using fmt package.

### Example

package main
//import the required packages
import (
"fmt"
"strconv"
"strings"
)

//function to get the hexadecimal number from string
func isDecimal(hexaString string) string {

// replace 0x or 0X with empty String
number := strings.Replace(hexaString, "0x", "", -1)
number = strings.Replace(number, "0X", "", -1)

// returns the hexadecimal number from a string
return number
}
func main() {
// initialize the string

// calling the isDecimal_num() function

// call ParseUint() function and pass the hexadecimal number as argument to it
output, err := strconv.ParseUint(result, 16, 64)

// in case of any error
if err != nil {
fmt.Println(err)
return
}

// printing the result on the screen
fmt.Println("The decimal conversion of", hexaDecimal_num, "is", output)
}


### Output

The decimal conversion of 0x12F is 303


### Description of the Code

• First we need to import the fmt, strconv and string packages respectively

• Make a hexa string function that will remove the 0x part from the hexadecimal number and will return the remaining number.

• Start the main() function.

• Initialize a variable of typr string and store the hexadecimal number to it that you wish to convert.

• Call the isDecimal_num() function and store its result.

• Pass the output of isDecimal_num() as the first argument of the ParseUint() function.

• Store the output of the function in output and err variables respectively.

• In case of an error print the error on the screen.

• Print the decimal number on the screen using fmt.Println() function.

## Conclusion

We have successfully compiled and executed a go language program to convert hexadecimal number to a decimal number.