- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
In go language all the hexadecimal numbers start with 0x or 0X. For example, 0x16F is a hexadecimal number
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() { // initialize the hexadecimal number hexadecimal_num := "23a" // 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) // initialize the hexadecimal number hexadecimal_num = "25f" //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) //initialize the hexadecimal number hexadecimal_num = "ffff" // 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 var hexaDecimal_num string hexaDecimal_num = "0x12F" // calling the isDecimal_num() function result := isDecimal(hexaDecimal_num) // 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.
- Related Articles
- Golang Program to convert Decimal to Hexadecimal
- Haskell Program to convert Decimal to Hexadecimal
- Swift Program to convert Hexadecimal to Decimal
- Swift Program to convert Decimal to Hexadecimal
- Haskell Program to convert Hexadecimal to Decimal
- Java Program to convert from decimal to hexadecimal
- Java program to convert decimal number to hexadecimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert decimal integer to hexadecimal number
- Golang Program to Convert Numeric Data to Hexadecimal
- How to Convert Decimal to Hexadecimal?
- How to Convert Hexadecimal to Decimal?
- Golang Program to convert Decimal to Octal
- How to convert Decimal to Hexadecimal in Java
- C++ program for hexadecimal to decimal
