简体   繁体   English

将 Unicode 中的结果传递给 Swift 中的字符串

[英]Deferent result in Unicode to String in Swift

I am translating my String value to unicodeScalars with this down code, for example if I gave "A" as input I will get 65 as output, now that I know 65 is number of A , then I want give 65 to receive A again, but it gave e , I am sure I am mixing some topic together and also missing something, need some help for understanding why this is deferent and how can I use "\u{?????}" for returning my String, thanks我正在使用此向下代码将我的String值转换为unicodeScalars ,例如,如果我将“A”作为输入,我将得到65作为 output,现在我知道65A的数量,那么我想给65再次接收A ,但它给了e ,我确信我将一些主题混合在一起并且还遗漏了一些东西,需要一些帮助来理解为什么这是不同的以及如何使用"\u{???}"来返回我的字符串,谢谢

let string: String = "A"
let UInt32Value: UInt32 = string.unicodeScalars[string.startIndex].value
print(UInt32Value) // 65


let newString = "\u{65}"
print(newString) // e

ps: I know that there is official way of using unicodescalar to get the String back, but I am interested to "\u{?????}" ps:我知道有使用 unicodescalar 来取回字符串的官方方法,但我对 "\u{?????}" 感兴趣

try to convert as follows尝试转换如下

let ans =  String(UnicodeScalar(UInt8(65)))
print(ans)

I think the problem is, you are converting (A) string -> unicodescalar -> uint我认为问题是,您正在转换 (A) 字符串 -> unicodescalar -> uint

and have to convert (65) uint -> unicodescalar -> string to get the original value并且必须转换 (65) uint -> unicodescalar -> string 以获得原始值

The \u{...} escape sequence uses hex: \u{...}转义序列使用十六进制:

Special characters can be included in string literals of both the single-line and multiline forms using the following escape sequences:使用以下转义序列,可以在单行和多行 forms 的字符串文字中包含特殊字符:

  • [...] [...]
  • Unicode scalar ( \u{n} ), where n is a hexadecimal number that has one to eight digits Unicode 标量 ( \u{n} ),其中 n 是具有一到八位的十六进制数

From the language reference .语言参考

So you need所以你需要

"\u{41}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM