I’ve successfully set up a small XZ compressor which returns a std::string that contains the compressed output. To process the result I need to “convert” the std::string to a NSString. Unfortunately there are (encoding?) problems:
Even though “abc” is not a good example it shows the difficulty pretty good:
NSString *text = [_text string]; // Length is 3
std::string content = xz_compress([text UTF8String]); // Length is 60
NSString *convertedContent = [NSString stringWithCString:content.c_str() encoding:NSUTF8StringEncoding];
// convertedContent is (null) and 0 characters long
Using NSUnicodeStringEncoding makes the NSString at least 2 characters long but they don’t match the ones from std::string.
My questions:
- Is this way possible / the preferable way (already unsure about that)?
- If so: What encoding do I need to use?
Thanks in advance!
Paul
I assume that xz_compress output is binary data. So, why don’t you try and use
NSDatadataWithBytes:length:method? Possibly you could also try withstring::data()instead ofstring::c_strfor the same reason: