I'm scanning a QR-Code with chinese characters encoded in Big5. (主页概况)
Is there a chance to get this String decoded correctly in Swift 3?
I found this Objective-C example on GitHub and this SO question, but there are no kCFStringEncodingBig5_HKSCS_1999
and kCFStringEncodingBig
constants in Swift.
Update:
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) ?? "?"))
}
Output: Big5 encoded String: \326\367\322\263\270\305\277\366
How can i get to the expected output Big5 encoded String: 主页概况
Update 2:
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 傳統
When I scan this code with my iOS app, metadata.stringValue returns the japanese String カヌイホ
What the hell is going on here???
CFStringEncodings
are defined as enumeration values in Swift 3:so you have to convert
Example:
Then
big5encoding
can be used for conversion betweenString
and(NS)Data
.In your case you have a string where each unicode scalar corresponds to a byte of the Big5 encoding. Then the following should work:
Alternatively, using the fact that the ISO Latin 1 encoding maps the Unicode code points U+0000 .. U+00FF to the bytes 0x00 .. 0xFF: