Golang 十进制转 ASCII 码:简单易懂的示例
在 Go 语言中,可以使用 strconv 包中的 Atoi 函数将十进制字符串转换为整数,然后使用 fmt.Sprintf 函数将整数转换为 ASCII 码。
示例代码:
package main
import (
"fmt"
"strconv"
)
func main() {
dec := '65'
i, err := strconv.Atoi(dec)
if err != nil {
fmt.Println("Error:", err)
return
}
ascii := fmt.Sprintf('%c', i)
fmt.Println("ASCII:", ascii)
}
输出结果:
ASCII: A
代码解释:
strconv.Atoi(dec):将十进制字符串dec转换为整数i。fmt.Sprintf('%c', i):使用%c格式化字符串,将整数i转换为 ASCII 码字符,并将其存储在ascii变量中。
通过以上步骤,您就可以轻松地将十进制字符串转换为 ASCII 码。
原文地址: http://www.cveoy.top/t/topic/f2SH 著作权归作者所有。请勿转载和采集!