Golang程序 将十进制转换为十六进制

Golang程序 将十进制转换为十六进制

在这篇文章中,你将学习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步– 导入 fmtstrconv 包。

  • 第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。

  • 将结果存储在一个单独的变量中。在这个例子中,我们使用了输出变量来存储结果。

  • 然后我们可以使用fmt.Println()函数将结果打印在屏幕上。

例2:使用FormatUint()函数将十进制数字转换成十六进制的Go语言程序

语法

func FormatUint(x uint64, base int) string

func FormatUint(x int64, base int) string - FormatInt()函数用于将整数值转换为另一个基值。这个函数需要两个参数,一个是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。

  • 将结果存储在一个单独的变量中。输出将是一个无符号整数变量。在这个例子中,我们使用了输出变量来存储结果。

  • 然后我们可以使用 fmt.Println() 函数在屏幕上打印结果。

总结

我们已经成功地编译并执行了go语言程序,将一个十进制数字转换为十六进制数字,并列举了一些预定义的函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程