NSString 特殊字符编码
我正在尝试转换一些特殊字符,例如 ä
、ö
、ü
、α
、μ、
α
、ο
、ι
以及网页中的其他内容。当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此链接找到的内容。它使用 CFXML 解析器的内置方法。它描述了下面的代码
或者您可以使用
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
Alternatively you can use
NSString* sI = (NSString*)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)s, NULL);
which is available depending on which OS you are building for.您也可以检查并使用它: https://github .com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m
使用此方法检查:
Also you can check this out and use it: https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m
Check using this method:
在再次尝试 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