Go语言程序:十进制转换为十六进制
本文将学习 Go 语言代码如何将十进制数转换为十六进制数。
十进制数与十六进制数
十进制数是以 10 为基数的数,这意味着数字的每一位都可以具有从 0 到 9(10 种可能性)的整数值,具体取决于其位置。例如,23 是一个十进制数。
任何包含 16 位数字的数字都基于 16 为基数,这意味着十六进制数包含从 0 到 9 的十进制数以及额外的 6 个字母 A-F。
在 Go 语言中,所有十六进制数都以 0x 或 0X 开头。例如,0x16F 是一个十六进制数。
示例 1:使用库函数将十进制数转换为十六进制数的 Go 语言程序
语法
func FormatInt(x uint64, base int) string
func FormatInt(x int64, base int) string − FormatInt() 函数用于将整数值转换为其他基数的值。此函数接受两个参数:一个是 64 位整数值,另一个是要将整数转换为的目标基数值。然后,此函数将最终结果作为字符串返回,我们可以将其存储在一个单独的变量中并在屏幕上打印。
算法
步骤 1 − 导入 `fmt` 和 `strconv` 包。
步骤 2 − 开始 `main()` 函数。
步骤 3 − 初始化要转换为十六进制的十进制数。
步骤 4 − 将十进制值作为参数传递给 `strconv.FormatInt()` 函数。
步骤 5 − 将结果存储在一个输出变量中。
步骤 6 − 最后使用 fmt 包打印结果。
示例
//GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING LIBRARY FUNCTION package main //import the required packages import ( "fmt" "strconv" ) // call the main function func main() { // initialize the decimal number and assign value to it var decimal int64 // let us convert 570 to hexadecimal decimal = 570 // use the FormatInt() function to convert decimal to hexadecimal // store the result in output variable output := strconv.FormatInt(decimal, 16) // print the results fmt.Println("The hexadecimal conversion of", decimal, "is", output) // initialize the second decimal number var decimal1 int64 decimal1 = 656565 // use the FormatInt() function to convert decimal to hexadecimal // store the result in output variable output1 := strconv.FormatInt(decimal1, 16) // print the results fmt.Println("The hexadecimal conversion of", decimal1, "is", output1) }
输出
The hexadecimal conversion of 570 is 23a The hexadecimal conversion of 656565 is a04b5
代码说明
首先,我们必须导入 fmt 包和 strconv 包。fmt 包允许我们在屏幕上打印任何内容,而 strconv 方法包含我们可以用来将十进制数转换为十六进制数的函数。
然后,我们需要初始化一个 64 位整数类型变量来存储我们希望转换的整数值。
在这个示例中,为了将整数转换为十六进制,我们将使用 `strconv.FormatInt()` 函数。
将您希望转换的整数值作为第一个参数传递给函数,并将基数 16 作为第二个参数传递给函数。
将结果存储在一个单独的变量中。在这个示例中,我们使用 output 变量来存储结果。
然后,我们可以使用 `fmt.Println()` 函数在屏幕上打印结果。
示例 2:使用 FormatUint() 函数将十进制数转换为十六进制数的 Go 语言程序
语法
func FormatUint(x uint64, base int) string
func FormatUint(x uint64, base int) string − FormatUint() 函数用于将无符号整数值转换为其他基数的值。此函数接受两个参数:一个是 64 位无符号整数值,另一个是要将整数转换为的目标基数值。然后,此函数将最终结果作为字符串返回,我们可以将其存储在一个单独的变量中并在屏幕上打印。
算法
步骤 1 − 导入 `fmt` 和 `strconv` 包。
步骤 2 − 开始 `main()` 函数。
步骤 3 − 初始化要转换为十六进制的十进制数。
步骤 4 − 将十进制值通过 `strconv.FormatUint()` 函数传递。
步骤 5 − 将结果存储在一个输出变量中。
步骤 6 − 最后使用 fmt 包打印结果。
示例
// GO LANGUAGE PROGRAM TO CONVERT THE DECIMAL NUMBER TO HEXADECIMAL USING FormatUint() FUNCTION package main //import the required packages import ( "fmt" "strconv" ) // calling the main function func main() { // initialize the decimal number var decimal uint64 // assign value to the integer variable decimal = 90 // use the FormatUint() function to convert output := strconv.FormatUint(decimal, 16) // print the decimal results fmt.Println("The hexadecimal conversion of", decimal, "is", output) // initialize the decimal number var decimal1 uint64 decimal1 = 1767 // use the FormatUint() function to convert output1 := strconv.FormatUint(decimal1, 16) // print the decimal results fmt.Println("The hexadecimal conversion of", decimal1, "is", output1) }
输出
The hexadecimal conversion of 90 is 5a The hexadecimal conversion of 1767 is 6e7
代码说明
首先,我们必须导入 fmt 包和 strconv 包。fmt 包允许我们在屏幕上打印任何内容,而 strconv 方法包含我们可以用来将十进制数转换为十六进制数的函数。
然后,我们需要初始化一个 64 位整数类型变量来存储我们希望转换的整数值。
在这个示例中,为了将整数转换为十六进制,我们将使用 `strconv.FormatUint()` 函数。
将您希望转换的整数值作为第一个参数传递给函数,并将基数 16 作为第二个参数传递给函数。
将结果存储在一个单独的变量中。输出将是一个无符号整数变量。在这个示例中,我们使用 output 变量来存储结果。
然后,我们可以使用 `fmt.Println()` 函数在屏幕上打印结果。
结论
我们已经成功编译并执行了 Go 语言程序,该程序将十进制数转换为十六进制数,并附带预定义函数的示例。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP