How to convert a XZ compression output to a NSString?
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::strin开发者_开发技巧g.
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 NSData
dataWithBytes:length:
method? Possibly you could also try with string::data()
instead of string::c_str
for the same reason:
NSData* convertedContent = [NSData dataWithBytes:content.data() length:content.length()];
精彩评论