Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">//GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING LIBRARY FUNCTION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">//import the required packages</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token string">"strconv"</span>
<span class="token punctuation">)</span>
<span class="token comment">// call the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// initialize the decimal number and assign value to it</span>
<span class="token keyword">var</span> decimal int64
<span class="token comment">// let us convert 570 to hexadecimal</span>
decimal <span class="token operator">=</span> <span class="token number">570</span>
<span class="token comment">// use the FormatInt() function to convert decimal to hexadecimal</span>
<span class="token comment">// store the result in output variable</span>
output <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatInt</span><span class="token punctuation">(</span>decimal<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>
<span class="token comment">// print the results</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The hexadecimal conversion of"</span><span class="token punctuation">,</span> decimal<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> output<span class="token punctuation">)</span>
<span class="token comment">// initialize the second decimal number</span>
<span class="token keyword">var</span> decimal1 int64
decimal1 <span class="token operator">=</span> <span class="token number">656565</span>
<span class="token comment">// use the FormatInt() function to convert decimal to hexadecimal</span>
<span class="token comment">// store the result in output variable</span>
output1 <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatInt</span><span class="token punctuation">(</span>decimal1<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>
<span class="token comment">// print the results</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The hexadecimal conversion of"</span><span class="token punctuation">,</span> decimal1<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> output1<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING FormatUint() FUNCTION</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">//import the required packages</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token string">"strconv"</span>
<span class="token punctuation">)</span>
<span class="token comment">// calling the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// initialize the decimal number</span>
<span class="token keyword">var</span> decimal uint64
<span class="token comment">// assign value to the integer variable</span>
decimal <span class="token operator">=</span> <span class="token number">90</span>
<span class="token comment">// use the FormatUint() function to convert</span>
output <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatUint</span><span class="token punctuation">(</span>decimal<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>
<span class="token comment">// print the decimal results</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The hexadecimal conversion of"</span><span class="token punctuation">,</span> decimal<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> output<span class="token punctuation">)</span>
<span class="token comment">// initialize the decimal number</span>
<span class="token keyword">var</span> decimal1 uint64
decimal1 <span class="token operator">=</span> <span class="token number">1767</span>
<span class="token comment">// use the FormatUint() function to convert</span>
output1 <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatUint</span><span class="token punctuation">(</span>decimal1<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>
<span class="token comment">// print the decimal results</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The hexadecimal conversion of"</span><span class="token punctuation">,</span> decimal1<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> output1<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
