在 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

代码解释:

  1. strconv.Atoi(dec):将十进制字符串 dec 转换为整数 i
  2. fmt.Sprintf('%c', i):使用 %c 格式化字符串,将整数 i 转换为 ASCII 码字符,并将其存储在 ascii 变量中。

通过以上步骤,您就可以轻松地将十进制字符串转换为 ASCII 码。


原文地址: http://www.cveoy.top/t/topic/f2SH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录