如何将NSString编码转换为UTF8

时间:2022-10-29 13:24:14

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.

提前谢谢你。

5 个解决方案

#1


4  

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:方法。

#2


3  

Try this one

试试这个

  NSString *s = @"Some string";
  const char *c = [s UTF8String];

#3


1  

import

进口

#import "NSString+URLEncoding.h" and
#import "NSString+URLEncoding.m" files

after that where u r doing encode write in .h file this method

之后,u r对。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);

#4


0  

Did you try [mystr UTF8String] ? This returns a char *

你试过[mystr f8string]吗?这返回一个char *

#5


-2  

You can try this 1) NSString to NSData(NSWindowsCP1251StringEncoding

您可以尝试这个1)NSString到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);

#1


4  

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:方法。

#2


3  

Try this one

试试这个

  NSString *s = @"Some string";
  const char *c = [s UTF8String];

#3


1  

import

进口

#import "NSString+URLEncoding.h" and
#import "NSString+URLEncoding.m" files

after that where u r doing encode write in .h file this method

之后,u r对。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);

#4


0  

Did you try [mystr UTF8String] ? This returns a char *

你试过[mystr f8string]吗?这返回一个char *

#5


-2  

You can try this 1) NSString to NSData(NSWindowsCP1251StringEncoding

您可以尝试这个1)NSString到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);