Golang 将数字数据转换为十六进制

Golang 将数字数据转换为十六进制

在本教程中,我们学习如何使用 Go 编程语言将数字数据转换为十六进制数。

十六进制数系统可描述为从 0 – 9 的数字以及从 A – F 的字母来表示的一种 16 位数字表示方法。换句话说,前 9 个数字或字母用数字表示,而接下来的 6 个数字用 A – F 的符号表示。

示例 1:使用 fmt.Sprintf() 函数将数据转换为十六进制数的 Golang 程序代码

语法

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

fmt.Sprintf() 函数是Go语言中的一个函数,它根据格式说明符进行格式化,并返回结果字符串。该函数定义在fmt包中。

此函数接受两个参数:格式字符串和一个…interface {},并返回结果字符串。

步骤

  • 步骤1 - 导入fmt包。

  • 步骤2 - 开始函数 main()

  • 步骤3 - 声明并初始化整数变量。

  • 步骤4 - 调用函数 fmt.Sprintf() 计算十六进制值。

  • 步骤5 - 使用 fmt.Printf() 打印结果。

示例

// 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
}

输出

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

代码描述

  • 在上面的程序中,我们首先声明了main包。main包用于告诉Go语言编译器该包必须编译并生成可执行文件。

  • 我们导入了fmt包,该包包含了fmt包的文件,然后我们可以使用与fmt包相关的函数。

  • 现在开始main()函数,这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何值。

  • 声明整数变量并将变量’value’初始化为您要转换为十六进制的整数值。

  • 接下来我们调用函数fmt.Sprintf(),该函数根据格式说明符进行格式化,并返回结果字符串。

  • 当我们在fmt.Sprintf()中使用%x时,结果以小写形式打印,当我们在fmt.Sprintf()中使用%X时,结果以大写形式打印。

  • 最后,使用fmt.Printf()将结果打印到屏幕上,该函数根据格式说明符进行格式化,并写入标准输出。

示例2:使用strconv包中定义的strconv.FormatInt()函数将数据转换为十六进制数的Golang程序代码

语法

func FormatInt (i int64, base int) string

FormatInt将值转换为字符串。FormatInt返回给定基数下的i的字符串表示形式,其中2 <= base <= 36。结果使用小写字母’a’到’z’表示大于等于10的数字值。

循环开始之前,范围表达式会被计算一次。

步骤

  • 第1步 - 导入fmt包和strconv包

  • 第2步 - 开始main()函数

  • 第3步 - 声明并初始化整数变量

  • 第4步 - 调用strconv.FormatInt()函数计算十六进制值

  • 第5步 - 使用fmt.Println()打印结果。

示例

// 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)
}

输出

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

代码描述

  • 在以上程序中,我们首先声明了 package main

  • 我们导入了 fmt 包,该包包含了 fmt 和 strconv 包的文件,后者实现了基本数据类型的字符串表示与转换。

  • 现在我们开始定义了 main() 函数 ,该函数是可执行程序的入口点。它不需要任何参数,也不返回任何值。

  • 声明一个整数变量 num,并将其初始化为所需查找其十六进制数的值。

  • 然后我们通过调用 strconv.FormatInt() 函数来计算其十六进制值。FormatInt 函数将值转换为字符串,并返回 num 变量的字符串表示。

  • 最后,我们使用 fmt.Println() 函数将结果打印在屏幕上,该函数使用默认格式为其操作数进行格式化,并写入标准输出。

结论

在以上两个示例中,我们已成功编译并执行了 Golang 程序代码,将数据转换为十六进制。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程