问题描述
我正在扫描一个用Big5编码的汉字的QR码. (大师概况)
I'm scanning a QR-Code with chinese characters encoded in Big5. (主页概况)
是否有机会在Swift 3中正确解码此String?
Is there a chance to get this String decoded correctly in Swift 3?
我在GitHub上找到了这个 Objective-C示例和这个,但没有kCFStringEncodingBig5_HKSCS_1999
和kCFStringEncodingBig
Swift中的常量.
I found this Objective-C example on GitHub and this SO question, but there are no kCFStringEncodingBig5_HKSCS_1999
and kCFStringEncodingBig
constants in Swift.
更新:
我找到了相应的swift变量,因此我现在尝试了以下操作:
I found the corresponding swift variables, so i now tried the following:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
guard metadataObjects?.count ?? 0 > 0 else {
return
}
guard let metadata = metadataObjects.first as? AVMetadataMachineReadableCodeObject, let code = metadata.stringValue else {
return
}
let big5encoding = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.big5.rawValue)))
print("Big5 encoded String: " + (String(data: code.data(using: .nonLossyASCII)!, encoding: big5encoding) ?? "?"))
}
输出:Big5 encoded String: \326\367\322\263\270\305\277\366
如何获得预期的输出Big5 encoded String: 主页概况
更新2:
看来我的QR码包含一些损坏的数据,所以我创建了一个新的Code,这次的内容绝对是Big5编码的String(Android应用正确读取了它).内容是傳統
It seems that my QR-Code contained some corrupt data, so i created a new Code, this time the content is definitely a Big5 encoded String (Android App reads it correctly). The content is 傳統
当我使用iOS应用程序扫描此代码时,metadata.stringValue返回日文字符串カヌイホ
When I scan this code with my iOS app, metadata.stringValue returns the japanese String カヌイホ
这到底是怎么回事?
推荐答案
CFStringEncodings
一个>在Swift 3中被定义为枚举值:
CFStringEncodings
are defined as enumeration values in Swift 3:
public enum CFStringEncodings : CFIndex {
// ...
case big5 /* Big-5 (has variants) */
// ...
case big5_HKSCS_1999 /* Big-5 with Hong Kong special char set supplement*/
// ...
}
所以你必须转换
CFStringEncodings -> CFStringEncoding -> NSStringEncoding -> String.Encoding
示例:
let cfEnc = CFStringEncodings.big5
let nsEnc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let big5encoding = String.Encoding(rawValue: nsEnc) // String.Encoding
然后big5encoding
可用于在String
和(NS)Data
之间进行转换.
Then big5encoding
can be used for conversion between String
and (NS)Data
.
在您的情况下,您有一个字符串,其中每个unicode标量对应于Big5编码的字节.然后,下面的方法应该起作用:
In your case you have a string where each unicode scalar corresponds toa byte of the Big5 encoding. Then the following should work:
// let code = "\u{00D6}\u{00F7}\u{00D2}\u{00B3}\u{00B8}\u{00C5}\u{00BF}\u{00F6}"
let bytes = code.unicodeScalars.map { UInt8(truncatingBitPattern: $0.value) }
if let result = String(bytes: bytes, encoding: big5encoding) {
print(result)
}
或者,使用ISO Latin 1编码映射的事实Unicode代码将U + 0000 .. U + 00FF指向字节0x00 .. 0xFF:
Alternatively, using the fact that the ISO Latin 1 encoding mapsthe Unicode code points U+0000 .. U+00FF to the bytes 0x00 .. 0xFF:
if let data = code.data(using: .isoLatin1),
let result = String(data: data, encoding: big5encoding) {
print(result)
}
这篇关于如何在iOS上的Swift中使用Big5编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!