Home > Software design >  Handling Windows Hebrew Encoding in Objective-c
Handling Windows Hebrew Encoding in Objective-c

Time:04-22

I am working on an ancient iOS app for a program that runs off VB6. It is passing strings over in a non unicode format including Hebrew characters which once they are parsed are displayed as "àáðø ãøåøé" I am assuming it is being encoded in Windows Hebrew.

I can't seem to find anything in the apple documentation that explains how to handle this case. And most searches bring up solutions in Swift, no Obj-C. I tried this:

NSString *hebrewPickup = [pickupText stringByApplyingTransform:NSStringTransformLatinToHebrew reverse:false];

But that just gave me this:

"ðø ַ̃øַ̊øֵ"

I am stumped.

EDIT: Based on JosefZ's comment I have tried to encode back using CP1252, but the issue is that CP1255 is not in the list of NSStringEncodings. But seems like it would solve my issue.

NSData *pickupdata = [pickupText dataUsingEncoding:NSWindowsCP1252StringEncoding];
NSString *convPick = [[NSString alloc] initWithData:pickupdata encoding:NSWindowsCP1254StringEncoding];
NSString *hebrewPickup = [convPick stringByApplyingTransform:NSStringTransformLatinToHebrew reverse:false];

CodePudding user response:

Ok, if any poor soul ends up here, this is how I ended up fixing it. I needed to add some Swift into my Obj-C code. (If only I could magically just rebuild the whole project in Swift instead.) Here is the info on that: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

Making use of this Swift Package: https://github.com/Cosmo/ISO8859

I added the following code to a new swift file.

@objc class ConvertString: NSObject {
    
    @objc func convertToHebrew(str:String) -> NSString {
        let strData = str.data(using: .windowsCP1252);
        let bytes: Data = strData!;
        if let test = String(bytes, iso8859Encoding: ISO8859.part8) {
            return test as NSString;
        }
        let test =  "";
        return test as NSString;
    }
}

Then in the Obj-C project I was able to call it like so:

ConvertString *stringConverter = [ConvertString new];
    
    NSString *pickupTextFixed = [stringConverter convertToHebrewWithStr:pickupText];
    NSString *deliverTextFixed = [stringConverter convertToHebrewWithStr:deliverText];
  • Related