I have a .DAT
file with Hebrew text, I want to decode the data. When trying to decode using text editors on Mac (BBEdit, Sublime or TextEdit) the Hebrew text becomes gibberish.
The file originally was in this format:
1081 310120 310120 197700 $ ‰˜…ˆ‰” .” .–…„ 01/20 01
And i've managed to change it to this format:
1081 310120 310120 197700 $ èâòÖàâî .î .ñÖÑ 01/20 01
When trying an online decoder: http://www.online-decoder.com/he it works and decode by default from macCroatian
to IBM862
, but there is no such encoding in Swift.
I found a library which I think that can help me do that, but can't add it to my project: https://www.example-code.com/swift3/load_text_file_using_code_page.asp
What is the best way to decode the.DAT file in Swift?
So after some research I came across this question which helped me put out this code from first answer:
if let filepath = Bundle.main.path(forResource: "somefile", ofType: "dat") {
do {
let encoding = "cp862" // String encoding
let converted = CFStringConvertIANACharSetNameToEncoding(encoding as CFString)
let encoded = CFStringConvertEncodingToNSStringEncoding(converted)
let sEncoding = String.Encoding(rawValue: se)
let contents = try String(contentsOfFile: filepath, encoding: sEncoding)
} catch {
// contents could not be loaded
}
} else {
// example.txt not found!
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.