开发者

NSString Decoding Problem

开发者 https://www.devze.com 2023-01-21 23:10 出处:网络
This String is base64 encoded string: NSString *string=@\"ë§ë ë¼ì´\"; This is not show the orginal string:

This String is base64 encoded string:

NSString *string=@"ë§ë ë¼ì´";

This is not show the orginal string:

NSLog(@"String is %@",[string cStringUsingEncoding:开发者_StackOverflow社区NSMacOSRomanStringEncoding]);


That's not a Base64-encoded string. There are a couple other things going on with your code, too:

  1. You can't include literal non-ASCII characters inside a string constant; rather, you have to use the bytes that make up the character, prefixed with \x; or in the case of Unicode, you can use the Unicode code point, prefixed with \u. So your string should look something like NSString *string = @"\x91\xa4\x91 \x91\x93";. But...

  2. The characters ¼ and ´ aren't part of the MacRoman encoding, so you'll have trouble using them. Are you sure you want a MacRoman string, rather than a Unicode string? Not many applications use MacRoman anymore, anyway.

  3. cStringUsingEncoding: returns a C string, which should be printed with %s, not %@, since it's not an Objective-C object.

That said, your code will sort of work with:

// Using MacRoman encoding in string constant
NSString *s = @"\x91\xa4\x91 \x91\x93";
NSLog(@"%s", [s cStringUsingEncoding:NSMacOSRomanStringEncoding]);

I say "sort of work" because, again, you can't represent that code in MacRoman.


That would be because Mac OS Roman is nothing like base-64 encoding. Base-64 encoding is a further encoding applied the bytes that represent the original string. If you want to see the original string, you will first need to base-64 decode the bytestring and then figure out the original string encoding in order to interpret it.

0

精彩评论

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

关注公众号