如何在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的信息,您可以浏览这些教程。