给定一个ASCII字符编码,如何将其编码为UTF-8呢?

huangapple go评论82阅读模式
英文:

Given an ASCII character code, how do I encode it as UTF-8?

问题

我需要将我的生成的ASCII字符代码转换为Go语言。

我的生成的代码如下:

  • 0(1-9缺失,可能无用)
  • 10(11-31缺失,可能更无用)
  • 32
  • 33
  • 34
  • ...
  • 124
  • 125
  • 126

如何将它们转换为相应的UTF-8编码字符?

英文:

I need to convert my generated ASCII character codes in Go.

My generated codes are as follows:

  • 0 (1-9 are missing, probably useless)
  • 10 (11-31 are missing, probably even more useless)
  • 32
  • 33
  • 34
  • ...
  • 124
  • 125
  • 126

How do I convert them to the corresponding UTF-8-encoded characters?

答案1

得分: 1

数字值是字节。你可以直接将它们转换为字符串。

b := []byte{97,98,99,68} // abcD的ASCII码
fmt.Println(string(b))
英文:

The numeric values are the bytes. You can directly convert those to strings.

b := []byte{97,98,99,68} // The ascii codes of abcD
fmt.Println(string(b))

huangapple
  • 本文由 发表于 2016年4月13日 11:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/36587918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定