Go語言程式:數值資料轉換為十六進位制
在本教程中,我們將學習如何使用 Go 程式語言將數值資料轉換為十六進位制數。
十六進位制數系統被描述為一種 16 位數字表示法,它使用 0-9 和 A-F 表示數字。換句話說,前 9 個數字用數字表示,接下來的 6 個數字用 A-F 的符號表示。
示例 1:使用 fmt.Sprintf() 函式將資料轉換為十六進位制數的 Go 語言程式程式碼
語法
func Sprintf(format string, a ...interface{}) string
Go 語言中的 **fmt.Sprintf()** 函式根據格式說明符進行格式化,並返回生成的字串。此函式在 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 時,結果以小寫字母列印;當我們使用 %X 時,結果以大寫字母列印。
最後,使用 **fmt.Printf()** 函式將結果列印到螢幕上,該函式根據格式說明符進行格式化並寫入標準輸出。
示例 2:使用 strconv 包中定義的 strconv.FormatInt() 函式將資料轉換為十六進位制數的 Go 語言程式程式碼
語法
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
程式碼描述
在上面的程式中,我們首先宣告 main 包
我們匯入了 fmt 包(包含 fmt 包的檔案)和 strconv 包(實現基本資料型別的字串表示形式的轉換)。
現在我們啟動 **函式 main ()**,此函式是可執行程式的入口點。它不接受任何引數也不返回任何值。
宣告整數變數 num 並將其初始化為您需要查詢其十六進位制數的值。
接下來,我們透過呼叫 **strconv.FormatInt ()** 函式計算十六進位制值。FormatInt 函式將值轉換為字串並返回 num 變數的字串表示形式。
最後,結果使用 **fmt.Println()** 函式列印到螢幕上,該函式使用其運算元的預設格式進行格式化並寫入標準輸出。
結論
在上面的兩個示例中,我們已成功編譯並執行了將資料轉換為十六進位制的 Go 語言程式程式碼。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP