如何在Go語言中列印ASCII值?
在本教程中,我們將學習如何在Go語言中查詢並列印任何字元或符號的ASCII值。ASCII代表美國資訊交換標準程式碼,它是一種以數字形式表示字元和符號的方法。
使用格式說明符列印ASCII值
演算法
步驟1 - 宣告字串型別的變數
步驟2 - 初始化變數。
步驟3 - 執行for迴圈,列印字串中每個元素的ASCII值。
示例1
在這個例子中,我們將使用%d格式說明符列印字元的ASCII值。
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the variable of string type using the var keyword var introduction string // initializing the introduction string introduction = "TutorialsPoint" fmt.Println("ASCII of ",introduction,"is") // printing the ASCII value of each character using %d specifier for i := 0; i < len(introduction); i++ { fmt.Printf("The ASCII value of %c is %d \n", introduction[i], introduction[i]) } fmt.Println("(Printing the ASCII value using specifier)") }
輸出
ASCII of TutorialsPoint is The ASCII value of T is 84 The ASCII value of u is 117 The ASCII value of t is 116 The ASCII value of o is 111 The ASCII value of r is 114 The ASCII value of i is 105 The ASCII value of a is 97 The ASCII value of l is 108 The ASCII value of s is 115 The ASCII value of P is 80 The ASCII value of o is 111 The ASCII value of i is 105 The ASCII value of n is 110 The ASCII value of t is 116 (Printing the ASCII value using specifier)
程式碼描述
var introduction string - 在這一行中,我們聲明瞭一個字串型別的變數introduction,我們將儲存使用者輸入。
for i := 0; i < len(introduction); i++ {} - 在整個字串上執行for迴圈,從0到字串長度。
fmt.Printf("The ASCII value of %c is %d \n", introduction[i], introduction[i]) - 使用%d格式說明符列印字串中每個元素的ASCII值
使用型別轉換列印ASCII值
演算法
步驟1 - 宣告字串型別的變數。
步驟2 - 透過建立reader物件來獲取使用者輸入
步驟3 - 執行for迴圈,列印字串中每個元素的ASCII值。
示例2
在這個例子中,我們將使用型別轉換列印字元的ASCII值。
package main // fmt package provides the function to print anything import ( "bufio" "fmt" "os" "strings" ) func main() { // declaring the variable of string type using the var keyword var dateOfBirth string fmt.Println("Can you please write down your Date of Birth?") // scanning the input by the user inputReader := bufio.NewReader(os.Stdin) dateOfBirth, _ = inputReader.ReadString('\n') // remove the delimiter from the string dateOfBirth = strings.TrimSuffix(dateOfBirth, "\n") // printing the ASCII value of each character using %d specifier for i := 0; i < len(dateOfBirth); i++ { fmt.Println("The ASCII value of", string(dateOfBirth[i]), "is", int(dateOfBirth[i])) } fmt.Println("(Printing the ASCII value using Type Casting)") }
輸出
Can you please write down your Date of Birth? 27/05/1993 The ASCII value of 2 is 50 The ASCII value of 7 is 55 The ASCII value of / is 47 The ASCII value of 0 is 48 The ASCII value of 5 is 53 The ASCII value of / is 47 The ASCII value of 1 is 49 The ASCII value of 9 is 57 The ASCII value of 9 is 57The ASCII value of 3 is 51 (Printing the ASCII value using Type Casting)
程式碼描述
var introduction string - 在這一行中,我們聲明瞭一個字串型別的變數introduction,我們將儲存使用者輸入。
inputReader := bufio.NewReader(os.Stdin)
introduction, _ = inputReader.ReadString('\n') - 建立一個reader物件並獲取使用者輸入。
for i := 0; i < len(introduction); i++ {} - 在整個字串上執行for迴圈,從0到字串長度。
fmt.Println("The ASCII value of", string(dateOfBirth[i]), "is", int(dateOfBirth[i])) - 使用型別轉換的概念(例如int(dateOfBirth[i]))列印字串中每個元素的ASCII值。
結論
這是在Go語言中列印ASCII值的兩種方法。要了解更多關於Go的資訊,您可以瀏覽這些教程。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP