Golang Program to convert Hexadecimal to Decimal (2024)

Golang Program to convert Hexadecimal to Decimal (1)

  • 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

Go ProgrammingServer Side ProgrammingProgramming

';

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 packagesimport ( "fmt" "strconv")// calling the main functionfunc 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 packagesimport ( "fmt" "strconv" "strings")//function to get the hexadecimal number from stringfunc 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.

Akhil Sharma

Updated on: 16-Nov-2022

7K+ Views

  • Related Articles
  • Golang 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
  • Haskell Program to convert Decimal to Hexadecimal
  • 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
  • Convert decimal integer to hexadecimal number in Java
  • Java program for Hexadecimal to Decimal Conversion
Kickstart Your Career

Get certified by completing the course

Get Started

Golang Program to convert Hexadecimal to Decimal (31)

Advertisem*nts

';

Golang Program to convert Hexadecimal to Decimal (2024)
Top Articles
Regarding IMEI Blacklist Problem - Apple Community
Understanding epistemology and its key approaches in research
How To Fix Epson Printer Error Code 0x9e
Somboun Asian Market
Patreon, reimagined — a better future for creators and fans
Plaza Nails Clifton
Mopaga Game
Driving Directions To Fedex
Chris wragge hi-res stock photography and images - Alamy
Brgeneral Patient Portal
Recent Obituaries Patriot Ledger
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Hover Racer Drive Watchdocumentaries
Valentina Gonzalez Leaked Videos And Images - EroThots
Goldsboro Daily News Obituaries
Skylar Vox Bra Size
5808 W 110Th St Overland Park Ks 66211 Directions
O'reilly's Auto Parts Closest To My Location
Lax Arrivals Volaris
Brett Cooper Wikifeet
Christina Steele And Nathaniel Hadley Novel
U Of Arizona Phonebook
SN100C, An Australia Trademark of Nihon Superior Co., Ltd.. Application Number: 2480607 :: Trademark Elite Trademarks
Gotcha Rva 2022
Apparent assassination attempt | Suspect never had Trump in sight, did not get off shot: Officials
Gen 50 Kjv
Jail Roster Independence Ks
Tu Housing Portal
Southtown 101 Menu
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
Frequently Asked Questions - Hy-Vee PERKS
Armor Crushing Weapon Crossword Clue
Current Time In Maryland
Storelink Afs
Unm Hsc Zoom
Baddies Only .Tv
A Small Traveling Suitcase Figgerits
Rocketpult Infinite Fuel
Drabcoplex Fishing Lure
Muma Eric Rice San Mateo
Edict Of Force Poe
Viewfinder Mangabuddy
Ludvigsen Mortuary Fremont Nebraska
062203010
Sams Gas Price Sanford Fl
Parent Portal Pat Med
Pulaski County Ky Mugshots Busted Newspaper
Iupui Course Search
Big Brother 23: Wiki, Vote, Cast, Release Date, Contestants, Winner, Elimination
Food and Water Safety During Power Outages and Floods
Craigslist Pets Lewiston Idaho
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 6247

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.