查找字节的UTF8字符(以十六进制表示)

时间:2017-06-28 21:49:05

标签: swift unicode utf-8

I already know Unicode中的代码点 U + 2014 以UTF8编码为 E2 80 94 hex

我希望看到UTF8中的角色。

我做了两次试验,都犯了同样的错误:

首先尝试

let bytes = [UInt8("94", radix: 16), UInt8("80", radix: 16), UInt8("E2", radix: 16) ]
let value = String(bytes: bytes, encoding: String.Encoding.utf8)

第二次尝试

let bytes = [0x94, 0x80, 0x16]
let value = String(bytes: bytes, encoding: String.Encoding.utf8)

我的游乐场出现了这个错误:

Playground execution failed: error: MyPlayground.playground:7:13: error: generic parameter 'S' could not be inferred
let value = String(bytes: bytes, encoding: String.Encoding.utf8)

P.S。我已经尝试从左到右交换十六进制值,但我仍然得到相同的错误。

我的错误是什么?

1 个答案:

答案 0 :(得分:1)

一旦你指定它是一个UInt8的数组,你的第二次尝试就可以了。

let bytes: [UInt8] = [0xE2, 0x80, 0x94]
let value = String(bytes: bytes, encoding: String.Encoding.utf8)

将正确的值放入数组中也很有帮助--E2,80,94,而不是94,80,16。