Golang Program to Convert Numeric Data to Hexadecimal


In the tutorial, we learn how to convert numeric data to hexadecimal number using Go programming language.
The hexadecimal number system is described as a 16-digit number representation of numbers from 0 - 9 and digits from A - F. In other words, the first 9 numbers or digits are represented as numbers while the next 6 digits are represented as symbols from A - F.

Example 1: Golang Program Code to Convert Data to Hexadecimal Number Using fmt.Sprintf() Function

Syntax

func Sprintf(format string, a ...interface{}) string

The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. This function is defined under the fmt package.

This function accepts two parameters format string and a…interface {} and it returns the resulting string.

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start function main ().

  • Step 3 − Declare and initialize the integer variable.

  • Step 4 − Calculate the hexadecimal value by calling the function fmt.Sprintf().

  • Step 5 − Print the result using fmt.Printf().

Example

// GOLANG PROGRAM TO CONVERT DATA TO HEXADECIMAL package main // fmt package allows us to print anything on the screen // fmt.Sprint function is defined under the fmt package import "fmt" // start the function main () // this function is the entry point of the executable program func main() { fmt.Println("Golang Program to convert data to hexadecimal") // initialize the integer variable int_value := 321 // calculate the hex value by calling the function fmt.Sprintf() // %x prints the hexadecimal characters in lowercase hex_value := fmt.Sprintf("%x", int_value) fmt.Printf("Hex value of %d is = %s\n", int_value, hex_value) // %X prints the hexadecimal characters in uppercase hex_value = fmt.Sprintf("%X", int_value) fmt.Printf("Hex value of %d is = %s\n", int_value, hex_value) // initialize the integer variable int_value = 6987 // calculate the hex value by calling the function fmt.Sprintf() // %x prints the hexadecimal characters in lowercase hex_value = fmt.Sprintf("%x", int_value) fmt.Printf("Hex value of %d is = %s\n", int_value, hex_value) // %X prints the hexadecimal characters in uppercase hex_value = fmt.Sprintf("%X", int_value) fmt.Printf("Hex value of %d is = %s\n", int_value, hex_value) // print the result }

Output

Golang Program to convert data to hexadecimal
Hex value of 321 is = 141
Hex value of 321 is = 141
Hex value of 6987 is = 1b4b
Hex value of 6987 is = 1B4B

Description of the Code

  • In the above program, we first declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file.

  • We imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Declare the integer variable and initialize the variable ‘value’ to an integer value you want to convert to hexadecimal.

  • Next we call the function fmt.Sprintf() which formats according to a format specifier and returns the resulting string.

  • When we use %x in fmt.Sprintf(), the result is printed in the lowercase and when we use %X in fmt.Sprintf(), the result is printed in the uppercase.

  • Finally print the result on the screen using fmt.Printf() which formats according to a format specifier and writes to standard output.

Example 2: Golang Program Code to convert data to Hexadecimal Number using strconv.FormatInt() Function which is Defined in strconv Package

Syntax

func FormatInt (i int64, base int) string

FormatInt converts values to strings. FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower−case letters 'a' to 'z' for digit values >= 10.

The range expression is evaluated once before the starting of the loop.

Algorithm

  • Step 1 − Import the package fmt and package strconv

  • Step 2 − Start the main () function

  • Step 3 − Declare and initialize the integer variable

  • Step 4 − calculate the hexadecimal value by calling the strconv.FormatInt() function

  • Step 5 − Print the result using fmt.Println().

Example

// GOLANG PROGRAM TO CONVERT DATA TO HEXADECIMAL package main // fmt package allows us to print anything on the screen // fmt.Sprint function is defined under the fmt package import ( "fmt" "strconv" ) // start the function main () // this function is the entry point of the executable program func main() { fmt.Println("Golang Program to convert data to hexadecimal") // declare a integer variable var num int64 // initialize the integer variable num = 11 // calculate the hex value by calling the strconv.FormatInt() function hex_num := strconv.FormatInt(num, 16) // print the result fmt.Printf("hexadecimal num of %d is %s", num, hex_num) num = 500 hex_num = strconv.FormatInt(num, 16) fmt.Printf("\nhexadecimal num of %d is %s", num, hex_num) }

Output

Golang Program to convert data to hexadecimal
hexadecimal num of 11 is b
hexadecimal num of 500 is 1f4

Description of the Code

  • In the above program, we first declare the package main

  • We imported the fmt package that includes the files of package fmt and strconv package which implements conversions to and from string representations of basic data types.

  • Now we start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Declare the integer variable num and initialize it to the value whose hexadecimal number you need to find.

  • Next we calculate the hexadecimal value by calling the strconv.FormatInt () function. FormatInt function converts values to strings and returns the string representation of the num variable.

  • Finally the result is printed on the screen using fmt.Println() function which formats using the default formats for its operands and writes to standard output.

Conclusion

We have successfully compiled and executed the Golang program code to convert data to hexadecimal in the above two examples.

Updated on: 15-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements