开发者

How to convert an NSString which is NSUTF8Encoded to NSASCIIEncoding in objective-c iPhone?

开发者 https://www.devze.com 2023-04-06 21:33 出处:网络
I have an NSString which contains data encoded with NSUTF8Encoding. I want to convert that string 开发者_Python百科into NSASCIIEncoding. Please tell me anyway to convert it in a proper manner. I am ab

I have an NSString which contains data encoded with NSUTF8Encoding. I want to convert that string 开发者_Python百科into NSASCIIEncoding. Please tell me anyway to convert it in a proper manner. I am able to convert reverse (NSASCIIEncoding to NSUTF8Encoding).

Please provide any sample code.

Thanks in advance


I have an NSString which contains data encoded with NSUTF8Encoding.

Really? Because an NSString always treats its contents as UTF-16 internally. Because you cannot be sure how an NSString stores its data internally. Conceptually, NSString works with UTF-16.

I want to convert that string into NSASCIIStringEncoding.

if ([myString canBeConvertedToEncoding:NSASCIIStringEncoding]) {
    const char *asciiString = [myString cStringUsingEncoding:NSASCIIStringEncoding];
}
0

精彩评论

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