Golang程序 将数字数据转换为十六进制数
在本教程中,我们将学习如何使用Go编程语言将数字数据转换为十六进制数。
十六进制数系统被描述为由0-9的数字和A-F的数字组成的16位数字表示。换句话说,前9位数字或数位被表示为数字,而后6位数字被表示为A-F的符号。
例1:使用fmt.Sprintf()函数将数据转换成十六进制数字的Golang程序代码
语法
func Sprintf(format string, a ...interface{}) string
Go语言中的 fmt.Sprintf() 函数根据格式指定符进行格式化,并返回结果字符串。这个函数被定义在fmt包下。
该函数接受两个参数format string和a…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’初始化为你想转换为16进制的整数值。
-
接下来我们调用函数 fmt.Sprintf() ,它根据格式指定符进行格式化,并返回结果字符串。
-
当我们在 fmt.Sprintf() 中使用%x时,结果将以小写打印,而当我们在 fmt.Sprintf() 中使用%X时,结果将以大写打印。
-
最后使用 fmt.Printf() 将结果打印在屏幕上,它根据格式指定符进行格式化并写入标准输出。
例2:使用strconv.FormatInt()函数将数据转换为十六进制数字的Golang程序代码,该函数在strconv包中定义。
语法
func FormatInt (i int64, base int) string
FormatInt将数值转换为字符串。FormatInt 返回 i 在给定基数下的字符串表示,对于 2 <= 基数 <= 36。对于数字值>=10,结果使用小写字母’a’到’z’。
范围表达式在循环开始前被评估一次。
算法
- 第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
代码的描述
-
在上面的程序中,我们首先声明了包main
-
我们导入了fmt包,该包包括fmt包和strconv包的文件,后者实现了基本数据类型的字符串表示法的转换。
-
现在我们启动 函数main() ,这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何东西。
-
声明整数变量num,并将其初始化为你需要找到的十六进制数的值。
-
接下来我们通过调用 strconv.FormatInt() 函数来计算十六进制的数值。FormatInt函数将数值转换为字符串,并返回num变量的字符串表示。
-
最后使用 fmt.Println() 函数将结果打印在屏幕上,该函数使用其操作数的默认格式进行格式化并写入标准输出。
总结
我们已经成功地编译并执行了Golang程序代码,将上述两个例子中的数据转换为十六进制。