NSString 特殊字符编码

发布于 2024-12-15 13:26:53 字数 1238 浏览 1 评论 0原文

我正在尝试转换一些特殊字符,例如 äöüαμ、αοι 以及网页中的其他内容。当我使用 ASIHTTPRequest 下载页面时,我得到一些代码而不是字符本身。示例:
ä = \u00E4
μ = \u03BC
α = \u03B1

如果我使用 [NSString stringWithContentsOfURL:aNSURL encoding:NSASCIIStringEncoding error:nil]; 也会发生这种情况 我尝试了不同的可用编码,但它们都不适用于上面的示例。例如:使用 NSUnicodeStringEncoding 我得到一些奇怪的“中文”字符,使用 NSASCIIStringEncoding 我得到这些数字和字母。

奇怪的是,如果我查看网页的源代码,在像 safari 这样的网络浏览器中,一切都很好,具有正常的 HTML 字符实体,例如: ä = & auml;

有什么方法可以将这些编码的字母转换回来吗?


谢谢

编辑
抱歉,我忘了提到上面浏览器的源代码。

我刚刚在这个网站上注意到: link 十六进制 HTML 实体与我用 tis 代码得到的非常相似。示例:
ä = ä
μ = μ
α = α

正如您可能看到的,它们非常相似。只需小写,0 替换为一个 x,并在开头添加 &#,末尾添加 ;。 我只需要编写一些小代码来将数字和字母转换为十六进制实体,这不会是一个大问题。然后只需使用 HTML 实体转换器即可完成。

再次帮助我

不管怎样,非常感谢肖恩

Im trying to convert some special characters like ä,ö,ü,α,μ,α,ο,ι, and others from a webpage. When I download the page with the ASIHTTPRequest i get some codes instead of the character itself. Examples:
ä = \u00E4
μ = \u03BC
α = \u03B1

This also happens if I use [NSString stringWithContentsOfURL:aNSURL encoding:NSASCIIStringEncoding error:nil];
I have tried different encodings available but none of them work for the above example. For example: With the NSUnicodeStringEncoding I get some strange like 'chinese' characters and with NSASCIIStringEncoding I get these numbers&letters.

The strange thing is, if I look in the source code, in a web browser like safari, of the webpage, it's all fine, with the normal HTML character entity like: ä = ä

Is there any way to convert these encoded letters back?

Thanks

EDIT
Sorry, that I forgot to mention the source code of a browser above.

I just noticed on this site: link that the hex HTML Entity is very similar to what I have got with tis code. Examples:
ä = ä
μ = μ
α = α

As you can maybe see, they are very similar. Just lowercase and the 0's are replaced with one x, and at the beginning add &#, to the end a ;.
I will just have to write some small code to convert the numbers&letters to the hex entities, not going to be a big problem. Then just have to use an HTML entity convertor and done.

Anyway, thanks a lot for helping me out again

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满意归宿 2024-12-22 13:26:53

您可以使用此链接找到的内容。它使用 CFXML 解析器的内置方法。它描述了下面的代码

@interface MREntitiesConverter : NSObject {
 NSMutableString* resultString;
}
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString)convertEntiesInString:(NSString)s;
@end

@implementation MREntitiesConverter
@synthesize resultString;
- (id)init
{
 if([super init]) {
 resultString = [[NSMutableString alloc] init];
 }
 return self;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
 [self.resultString appendString:s];
}
- (NSString)convertEntiesInString:(NSString)s {
 if(s == nil) {
 NSLog(@"ERROR : Parameter string is nil");
 }
 NSString* xmlStr = [NSString stringWithFormat:@"<d>%@</d>", s];
 NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
 NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data];
 [xmlParse setDelegate:self];
 [xmlParse parse];
 NSString* returnStr = [[NSString alloc] initWithFormat:@"%@",resultString];
 return returnStr;
}
- (void)dealloc {
 [resultString release];
 [super dealloc];
}
@end

或者您可以使用 NSString* sI = (NSString*)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)s, NULL); ,这取决于您正在构建的操作系统。

You can use the found at this link. It uses a built in method from the CFXML parser. It describes the code below

@interface MREntitiesConverter : NSObject {
 NSMutableString* resultString;
}
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString)convertEntiesInString:(NSString)s;
@end

@implementation MREntitiesConverter
@synthesize resultString;
- (id)init
{
 if([super init]) {
 resultString = [[NSMutableString alloc] init];
 }
 return self;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
 [self.resultString appendString:s];
}
- (NSString)convertEntiesInString:(NSString)s {
 if(s == nil) {
 NSLog(@"ERROR : Parameter string is nil");
 }
 NSString* xmlStr = [NSString stringWithFormat:@"<d>%@</d>", s];
 NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
 NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data];
 [xmlParse setDelegate:self];
 [xmlParse parse];
 NSString* returnStr = [[NSString alloc] initWithFormat:@"%@",resultString];
 return returnStr;
}
- (void)dealloc {
 [resultString release];
 [super dealloc];
}
@end

Alternatively you can use NSString* sI = (NSString*)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)s, NULL); which is available depending on which OS you are building for.

一人独醉 2024-12-22 13:26:53

您也可以检查并使用它: https://github .com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByLinkifyingURLs;

使用此方法检查:

- (NSString *)stringByDecodingHTMLEntities;

Also you can check this out and use it: https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByLinkifyingURLs;

Check using this method:

- (NSString *)stringByDecodingHTMLEntities;
守不住的情 2024-12-22 13:26:53

在再次尝试 Rob Mayoff 的代码后,它成功了!这是他的回答的链接:
转换转义的 UTF8 字符回到原来的样子

After having another try with Rob Mayoffs code it worked! Here is the link to his answer:
Converting escaped UTF8 characters back to their original form

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文