Golang Example - Convert(cast) Hexadecimal to Decimal Integer Number vice versa


In this blog post, You will learn two programs in Go language, The first program is to convert Hexadecimal to Decimal Integer. The second program to convert Decimal to Hexadecimal Number.

Golang Hexadecimal Number System

Hexadecimal number  is also called Hexa number which contains 16 digits ie numbers start from 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. It is based on the Base 16 ie Hexa number system. So HexaDecimal contains Decimal Numbers,0-9 (base 10)  and  Extra 6 alphabets - A to F

 In Golang, Hexadecimal number always either prefixed with 0x or 0X or normal numbers, Example Hexadecimal number is 0x12F or 0X34A,23A

Decimal Number are numbers which contain numbers based on base 10. ie numbers starting from 0 to 9, Example decimal Number is 25

 The below programs takes input from a user keyboard console and stored in the corresponding variable.

Example Program to convert from Hexadecimal to Decimal Number

We have 2 ways to cast Hexadecimal to decimal number as follows

  • using strconv ParseInt function
  • Using strconv ParseUint function

strconv ParseInt function example to cast Hexa to Decimal Integer

strconv package provides ParseInt function which does the conversion of different types. A parseInt function is used to convert given string in Hexa format, convert to a Decimal number. Please note that the base supplied to this function is 16.

Given the input string is stored variable hexaNumber.

valid hexaNumber values accepts values - 23a,0x23a,0X23a.

if hexaNumber string prefixed with 0x, or 0X, strconv.ParseInt() throws error strconv.ParseInt: parsing “0x23A”: invalid syntax

So we have to write a function to replace 0x or 0X with empty string.

Here is a complete function to replace long hexadecimal numbers

func hexaNumberToInteger(hexaString string) string {  
 // replace 0x or 0X with empty String  
 numberStr := strings.Replace(hexaString, "0x", "", -1)  
 numberStr = strings.Replace(numberStr, "0X", "", -1)  
 return numberStr  
} 

Here is a complete example for Casting long Hexadecimal number to decimal Integer

package main  
  
import (  
 "fmt"  
 "math"  
 "strconv"  
 "strings"  
)  
func hexaNumberToInteger(hexaString string) string {  
 // replace 0x or 0X with empty String  
 numberStr := strings.Replace(hexaString, "0x", "", -1)  
 numberStr = strings.Replace(numberStr, "0X", "", -1)  
 return numberStr  
}  
  
func main() {  
 var hexaNumber string  
 fmt.Print("Enter Hexadecimal Number:")  
 fmt.Scanln(&hexaNumber)  
 output, err := strconv.ParseInt(hexaNumberToInteger(hexaNumber), 16, 64)  
 if err != nil {  
  fmt.Println(err)  
  return  
 }  
 fmt.Printf("Output %d", output)  
}  

When the above program is compiled and executed, Output is

Enter Hexadecimal Number:23a  
Output 570 

For Example, If the input number is given as 23a, Output is 570 Output is returned with calculated 2*16*16+3*16+10*16

strconv ParseUint function example to convert Hexadecimal to number 

strconv package provides various functions for signed and unsigned numbers Like ParseInt function, ParseUint function is used to convert unsigned integer ParseInt function is used to convert given string in Hexa format, convert to a Decimal number. Please note that the base supplied to this function is 16.
Here is an example program convert Hexadecimal to a decimal number of type uint64

package main  
  
import (  
 "fmt"  
 "strconv"  
 "strings"  
)  
  
func hexaNumberToInteger(hexaString string) string {  
 // replace 0x or 0X with empty String  
 numberStr := strings.Replace(hexaString, "0x", "", -1)  
 numberStr = strings.Replace(numberStr, "0X", "", -1)  
 return numberStr  
}  
  
func main() {  
 var hexaNumber string  
 fmt.Print("Enter Hexadecimal Number:")  
 fmt.Scanln(&hexaNumber)  
 output, err := strconv.ParseUint(hexaNumberToInteger(hexaNumber), 16, 64)  
 if err != nil {  
  fmt.Println(err)  
  return  
 }  
 fmt.Printf("Output %d", output)  
}  

The output of the above program is

Enter Hexadecimal Number:0x25  
Output 37  

Example Program to convert Decimal to HexaDecimal Number

We have 2 ways to convert Decimal to HexaDecimal number in Golang. Below are two ways as follows

  • using strconv FormatInt function 
  • using strconv FormatUint function 

The below program takes decimal input number from keyboard console, stores it in variable decimal.

strconv FormatInt function example to convert Decimal to Hexa Number

Inbuilt Standard package strconv provides FormatInt function used to convert Decimal to Hexa number. Here is an example program for Casting Decimal integer to Hexa number

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func main() {  
 var decimal int64  
 fmt.Print("Enter Decimal Number:")  
 fmt.Scanln(&decimal)  
 output := strconv.FormatInt(decimal, 16)  
 fmt.Print("Output ", output)  
  
}  

Output is

Enter Decimal Number:29  
Output 1d  

strconv FormatUint function example to convert Decimal to Hexadecimal Number

strconv package FormatUint function which converts of Uint64 to hexadecimal number. Here is an example program for Casting unsigned integer to Hexadecimal number.

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func main() {  
 var decimal uint64  
 fmt.Print("Enter Decimal Number:")  
 fmt.Scanln(&decimal)  
 output := strconv.FormatUint(decimal, 16)  
 fmt.Print("Output ", output)  
  
}  

Output is

Enter Decimal Number:30  
Output 1e  

Similar Posts