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 語言程式,該程式將十進位制數轉換為十六進位制數,並附帶預定義函式的示例。

更新於:2022年11月14日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告