- 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 Decimal to Hexadecimal
In this article you will learn the go language code to convert decimal number to Hexadecimal.
Decimal Number Vs Hexadecimal Numbers
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 9 (10 possibilities) depending on its position. For example, 23 is a decimal number.
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
Example 1: Go Language Program To Convert a Decimal Number Into Hexadecimal Using Library Function
Syntax
func FormatInt(x uint64, base int) string
func FormatInt(x int64, base int) string − FormatInt() function is used to convert integer values to another base values. This function takes two arguments one is the 64 bit integer value and another is the base value to which we want to convert the integer. This function then returns the final result as a string which we can store in a separate variable and print on the screen.
Algorithm
Step 1 − import the fmt and strconv package.
Step 2 − Start the main() function.
Step 3 − Initialize the decimal numbers to convert to hexadecimal
Step 4 − Pass the decimal value through strconv.FormatInt() function as an argument.
Step 5 − Store the result in a output variable
Step 6 − Finally print the results using fmt package.
Example
//GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING LIBRARY FUNCTION package main //import the required packages import ( "fmt" "strconv" ) // call the main function func main() { // initialize the decimal number and assign value to it var decimal int64 // let us convert 570 to hexadecimal decimal = 570 // use the FormatInt() function to convert decimal to hexadecimal // store the result in output variable output := strconv.FormatInt(decimal, 16) // print the results fmt.Println("The hexadecimal conversion of", decimal, "is", output) // initialize the second decimal number var decimal1 int64 decimal1 = 656565 // use the FormatInt() function to convert decimal to hexadecimal // store the result in output variable output1 := strconv.FormatInt(decimal1, 16) // print the results fmt.Println("The hexadecimal conversion of", decimal1, "is", output1) }
Output
The hexadecimal conversion of 570 is 23a The hexadecimal conversion of 656565 is a04b5
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 decimal number to hexadecimal number.
Then we need to initialize a 64 bit integer type variable to store the integer value that we wish to convert.
In this example to convert integer to hexadecimal we will be using strconv.FormatInt() function.
Pass the integer value that you wish to convert as the first argument to the function and the base value as the second argument to the function 16 in this case.
Store the result in a separate variable. In this example we have used output variable to store the result.
Then we can print the result on the screen using fmt.Println() function.
Example 2: Go Language Program To Convert Decimal Number to Hexadecimal Using FormatUint() Function
Syntax
func FormatUint(x uint64, base int) string
func FormatUint(x int64, base int) string − FormatInt() function is used to convert integer values to another base values. This function takes two arguments one is the 64 bit integer value and another is the base value to which we want to convert the integer. This function then returns the final result as a string which we can store in a separate variable and print on the screen.
Algorithm
Step 1 − import the fmt and strconv package.
Step 2 − Start the main() function.
Step 3 − Initialize the decimal numbers to convert to hexadecimal
Step 4 − Pass the decimal value through strconv.FormatUint() function.
Step 5 − Store the result in a output variable.
Step 6 − Finally print the results using fmt package.
Example
// GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING FormatUint() FUNCTION package main //import the required packages import ( "fmt" "strconv" ) // calling the main function func main() { // initialize the decimal number var decimal uint64 // assign value to the integer variable decimal = 90 // use the FormatUint() function to convert output := strconv.FormatUint(decimal, 16) // print the decimal results fmt.Println("The hexadecimal conversion of", decimal, "is", output) // initialize the decimal number var decimal1 uint64 decimal1 = 1767 // use the FormatUint() function to convert output1 := strconv.FormatUint(decimal1, 16) // print the decimal results fmt.Println("The hexadecimal conversion of", decimal1, "is", output1) }
Output
The hexadecimal conversion of 90 is 5a The hexadecimal conversion of 1767 is 6e7
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 decimal number to hexadecimal number.
Then we need to initialize a 64 bit integer type variable to store the integer value that we wish to convert.
In this example to convert integer to hexadecimal we will be using strconv.FormatUint() function.
Pass the integer value that you wish to convert as the first argument to the function and the base value as the second argument to the function 16 in this case.
Store the result in a separate variable. The output will be an unsigned integer variable. In this example we have used output variable to store the result.
Then we can print the result on the screen using fmt.Println() function.
Conclusion
We have successfully compiled and executed the go language program to convert a decimal number to hexadecimal one along with examples pre-defined functions.
- Related Articles
- Golang Program to convert Hexadecimal to Decimal
- 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
