Multiple ways to Convert Hexadecimal to/from Integer Number in Golang

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

Golang Hexadecimal Number System

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

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

Decimal Numbers are numbers that contain numbers based on base 10. i.e numbers starting from 0 to 9, Example decimal Number is 25

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

How to convert from Hexadecimal to Decimal Number in Golang?

We have 2 ways to cast Hexadecimal to decimal numbers as follows.

  • using strconv ParseInt function
  • Using strconv ParseUint function

cast Hexa to Decimal Intege using golang strconv ParseInt function example

strconv package provides the ParseInt function, which does the conversion of different types.

A parseInt function use to convert a given string in Hexa format, convert to a Decimal number.

Please note that the base supplied to this function is 16.

Given the input, the string stores the 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 an 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 a long Hexadecimal number to a 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, the Output is

Enter Hexadecimal Number:23a
Output 570

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

convert Hexadecimal to number using strconv ParseUint function example

strconv package provides various functions for signed and unsigned numbers.

Like ParseInt function, ParseUint function is used to convert unsigned integer. The ParseInt function is used to convert a 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 that converts 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

How to Convert Decimal to HexaDecimal Number with examples

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

  • using strconv FormatInt function
  • using strconv FormatUint function

The below program takes decimal input numbers from the keyboard console and stores them in variable type decimal.

convert Decimal to Hexa using golang strconv FormatInt function

Inbuilt Standard package strconv provides the 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:

Enter Decimal Number:29
Output 1d

convert Decimal to Hexadecimal Number using golang strconv FormatUint function example

strconv package FormatUint function which converts of Uint64 to hexadecimal number.

Here is an example program for Casting an unsigned integer to a 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:

Enter Decimal Number:30
Output 1e

Conclusion

You learned to Convert Hexadecimal to decimal number and number to hex using inbuilt functions and writing custom functions.