Golang 如何打印ASCII值
在本教程中,我们将学习如何在Golang中找到并打印任何字符或符号的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值
使用Tyoe Casting打印ASCII值
算法
-
第1步 – 声明字符串类型的变量。
-
第2步 – 通过为阅读器创建一个对象来获取用户的输入。
-
第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 ‘) – 创建一个阅读器对象并接受用户的输入。
- **for i := 0; i < len(introduction); i++ {} – **在完整的字符串上运行一个for循环,从0到字符串的长度。
-
fmt.Println(“The ASCII value of”, string(dateOfBirth[i]), “is”, int(dateOfBirth[i]) – 使用类型转换概念打印字符串中每个元素的ASCII值,如int(dateOfBirth[i])。
总结
以上是在Golang中打印ASCII值的两种方法。