ios 如何将 NSString 编码转换为 UTF8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8784549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert NSString encoding to UTF8
提问by JAHelia
I have done something like:
我做过类似的事情:
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
then NSLog(@"%@", str);
然后 NSLog(@"%@", str);
However, if 'mystr' is english then the NSLog
would print it as is, but if mystr
is Arabic (for ex.) NSLog
will not print anything, so how can i change the encoding of mystr
to UTF8 ?
但是,如果 'mystr' 是英语,那么NSLog
它会按原样打印,但如果mystr
是阿拉伯语(例如)NSLog
不会打印任何内容,那么我如何将编码更改mystr
为 UTF8 ?
thank you in advance.
先感谢您。
回答by
Your first line creates some data that is in cp1251 encoding. Your second line says "read this data into a string, assuming that the bytes represent a UTF8 encoded string". But because the bytes represent a cp1251 encoded string, that's not likely to work very well.
您的第一行创建了一些采用 cp1251 编码的数据。您的第二行说“将此数据读入字符串,假设字节表示 UTF8 编码字符串”。但是因为字节代表一个 cp1251 编码的字符串,所以不太可能很好地工作。
NSString represents an ordered collection of characters. Internally it uses some encoding to store these characters in memory, but its interface provides an encoding-independent access to the string and you can therefore consider NSString to be encoding-agnostic. If what you want is a collection of bytes that represent the string in UTF8 encoding, then you don't want an NSString. You want to get an NSString to emit such a collection of bytes, perhaps using the -dataUsingEncoding: method you've already found.
NSString 表示一个有序的字符集合。在内部,它使用一些编码将这些字符存储在内存中,但它的接口提供了对字符串的编码独立访问,因此您可以将 NSString 视为与编码无关。如果您想要的是代表 UTF8 编码字符串的字节集合,那么您不需要 NSString。您希望获得一个 NSString 来发出这样的字节集合,也许使用您已经找到的 -dataUsingEncoding: 方法。
回答by Anand
Try this one
试试这个
NSString *s = @"Some string";
const char *c = [s UTF8String];
回答by mahesh chowdary
import
进口
#import "NSString+URLEncoding.h" and
#import "NSString+URLEncoding.m" files
after that where u r doing encode write in .h file this method
在那之后你在 .h 文件中进行编码写入这个方法
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
after that write in .m file method implementation
之后写入 .m 文件方法实现
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding
{
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding)));
}
after that use like this
之后像这样使用
NSString *keyword=@"sample text";
here pass ur string whatever
在这里传递你的字符串
NSString *url = [NSString stringWithFormat:@"%@",[keyword urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",url);
回答by Shanti K
Did you try [mystr UTF8String]
? This returns a char *
你试了[mystr UTF8String]
吗?这将返回一个字符 *
回答by Musthafa P P
You can try this 1) NSString to NSData(NSWindowsCP1251StringEncoding
你可以试试这个 1) NSString to NSData(NSWindowsCP1251StringEncoding
NSString *text=@"This is Sample Text Conversion.....";
NSData *data=[text dataUsingEncoding:NSWindowsCP1251StringEncoding];
2)Revers process.
2) 逆过程。
NSString *textRev=[[NSString alloc]initWithData:data encoding:NSWindowsCP1251StringEncoding];
NSLog(@" Actual String.. %@",textRev);