Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Convert NSString encoding from WindowsCP1251 to UTF8

how can I have this conversion from NSWindowsCP1251StringEncoding to UTF-8? I had several attempts but no one worked as it should. My last try was:

NSData *dt = [mystr dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSWindowsCP1251StringEncoding];

The result of str is unreadable. Did anyone encounter anything similar?

like image 639
Bill Avatar asked Feb 09 '11 08:02

Bill


1 Answers

I think you were so close:

// Convert it back to CP1251
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];

// Now load it as UTF8
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
like image 187
Tom van der Woerdt Avatar answered Nov 15 '22 07:11

Tom van der Woerdt