开发者

How to convert a XZ compression output to a NSString?

开发者 https://www.devze.com 2023-03-24 08:18 出处:网络
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. Unfortunatel

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()];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号