Go 語言程式:將字串值轉換為位元組值
在本教程中,我們將學習如何使用 Go 程式語言將字串值轉換為位元組值。
字串值 − 字串值只是一系列字元,例如 "abc"。字串值始終用引號括起來。所有型別的字元都允許(即使是數字,如 "abc123")。字串可以包含任意數量的字元。
位元組值 − 位元組資料型別是 8 位有符號的二進位制補碼整數。其最小值為 -128,最大值為 127(含)。在大型陣列中,位元組資料型別可用於節省記憶體,在記憶體節省至關重要的場合很有用。
示例 1:使用 []byte(string) 將字串值轉換為位元組值的 Go 程式程式碼
語法
[]byte(string)
輸入 = 我們將字串傳遞給位元組切片。
輸出 = 返回字串中所有字元的位元組
演算法
步驟 1 − 匯入 fmt 包
步驟 2 − 啟動 main() 函式
步驟 3 − 宣告一個字串值
步驟 4 − 將字串值轉換為位元組值
步驟 5 − 在螢幕上列印位元組值切片
示例
package main //import format package import ( "fmt" ) // start the function main () // program execution starts here func main() { fmt.Println("GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE") //declare string s := "SWIFT" fmt.Println("String value entered:\n",s) fmt.Println("After converting to Bytes value:") //convert string to bytes b := []byte(s) //display the slice of bytes using fmt.Println () function fmt.Println(b) }
輸出
GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE String value entered: SWIFT After converting to Bytes value: [83 87 73 70 84]
描述
在上面的程式中,我們首先宣告 main 包。
我們匯入了包含 fmt 包檔案的 fmt 包。
現在啟動 main() 函式,此函式是可執行程式的入口點。它不接受任何引數,也不返回任何內容。
宣告要轉換為位元組值的字串值。在程式碼第 16 行:我們使用變數名宣告並初始化一個字串。
使用 []byte(string) 函式將字串值轉換為位元組值。在程式碼第 19 行:我們將字串 s 傳遞給位元組切片以將其轉換為位元組切片。我們將此位元組切片分配給變數 b
最後,我們使用 fmt.Println() 函式在螢幕上列印位元組切片,該函式使用其運算元的預設格式進行格式化並寫入標準輸出。
在此示例中,我們使用了 byte() 函式。位元組是 8 位無符號整數。byte() 函式以字串作為輸入並返回陣列。
示例 2:使用 Make() 函式和 Copy() 函式將字串值轉換為位元組值的 Go 程式程式碼
語法
func make(t Type, size ...IntegerType) Type func copy(dst, src []Type) int
演算法
步驟 1 − 匯入 fmt 包
步驟 2 − 啟動 main() 函式
步驟 3 − 宣告一個字串值並初始化它
步驟 4 − 使用 make() 函式將字串值轉換為位元組值
步驟 5 − 使用內建的 copy() 函式將元素複製到目標切片
步驟 6 − 在螢幕上列印位元組值切片
示例
package main //import format package import ( "fmt" ) // start the function main () // program execution starts here func main() { fmt.Println("GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE") // Declare a string variable var strToConvert string // initialize the string variable to be converted strToConvert = "JEEPERS CREEPERS" fmt.Println("Entered value =",strToConvert) // convert the string value to byte value using // the function make () byteString := make([]byte, 10) // The built-in copy function copies elements // into a destination slice dst from a source slice src copy(byteString, strToConvert) fmt.Println("After conversion of the string value to byte:") // print the result using fmt.Println () function fmt.Println(byteString) }
輸出
GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE Entered value = JEEPERS CREEPERS After conversion of the string value to byte: [74 69 69 80 69 82 83 32 67 82]
描述
在上面的程式中,我們首先宣告 main 包。
我們匯入了包含 fmt 包檔案的 fmt 包。
現在啟動 main() 函式,此函式是可執行程式的入口點。它不接受任何引數,也不返回任何內容。
宣告要轉換的字串值並初始化它。
使用內建函式 make() 將字串值轉換為位元組值。make() 函式用於切片、對映或通道。make() 在堆上分配記憶體並初始化並將零或空字串放入值中,並且 make() 返回與其引數相同的型別。
接下來,我們使用內建函式 copy() 將元素從源切片複製到目標切片 dst。它返回複製的元素數,這將是 len(dst) 和 len(src) 中的較小值。結果不取決於引數是否重疊。
最後,我們使用 fmt.Println() 函式在螢幕上列印位元組值切片,該函式使用其運算元的預設格式進行格式化並寫入標準輸出
結論
在以上兩個示例中,我們已成功編譯並執行了 Go 語言程式程式碼,以將字串值轉換為位元組值。在第一個示例中,我們使用了 []byte(string) 函式將字串值轉換為位元組值。
在第二個示例中,我們使用了 Go 語言的兩個內建函式,make() 函式和 copy() 函式,將字串值轉換為位元組值。