Golang example Convert(cast) Binary to/from Decimal
Convert Decimal Values to Hexadecim...
Convert Decimal Values to Hexadecimal - Excel Formula
DecimalHexadecimal

Golang Hexadecimal Number System

HexadecimalHexaBase 16HexaHexa Decimal
Hexadecimal0x0X

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?

Hexadecimaldecimal
strconv ParseIntstrconv ParseUint

cast Hexa to Decimal Intege using golang strconv ParseInt function example

strconvParseInt
HexaDecimal
16

Given the input, the string stores the variable hexaNumber.

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

strconv.ParseInt()strconv.ParseInt: parsing "0x23A": invalid syntax
0x

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

strconvsignedunsigned
ParseIntParseUintParseIntHexaDecimal

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

DecimalHexaDecimal
strconv FormatIntconv FormatUint
decimal

convert Decimal to Hexa using golang strconv FormatInt function

strconvFormatInt

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

Uint64hexadecimal

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

Hexadecimal