热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在iPhone上检测NSString中的Unicode字符-DetectUnicodecharactersinNSStringoniPhone

IamworkingonanSMSapplicationfortheiPhone.Ineedtodetectiftheuserhasenteredanyunic

I am working on an SMS application for the iPhone. I need to detect if the user has entered any unicode characters inside the NSString they wish to send.

我正在为iPhone开发一个SMS应用程序。我需要检测用户是否在他们希望发送的NSString中输入了任何unicode字符。

I need to do this is because unicode characters take up more space in the message, and also because I need to convert them into their hexadecimal equivalents.

我需要这样做是因为unicode字符占用了消息中更多的空间,也因为我需要将它们转换为十六进制等价物。

So my question is how do I detect the presence of a unicode character in an NSString (which I read from a UITextView). Also, how do I then convert those characters into their UCS‐2 hexadecimal equivalents?

所以我的问题是如何在NSString中检测到unicode字符的存在(我从UITextView中读取)。另外,如何将这些字符转换为UCS-2十六进制等效字符?

E.g 繁 = 7E41, 体 = 4F53, 中 = 4E2D, 文 = 6587

E.g繁= 7E41,体= 4F53,中= 4E2D,文= 6587

2 个解决方案

#1


25  

To check for only ascii characters (or another encoding of your choice) use:

要仅检查ascii字符(或您选择的其他编码),请使用:

[myString canBeConvertedToEncoding:NSASCIIStringEncoding];

It will return NO if the string contains non-ascii characters. You can then convert the string to UCS-2 data with:

如果字符串包含非ascii字符,它将返回NO。然后,您可以使用以下命令将字符串转换为UCS-2数据:

[myString dataUsingEncoding:NSUTF16BigEndianStringEncoding];

or NSUTF16LittleEndianStringEncoding depending on your platform. There are slight differences between UCS-2 and UTF-16. UTF-16 has superseded UCS-2. You can read about the differences here:

或NSUTF16LittleEndianStringEncoding,具体取决于您的平台。 UCS-2和UTF-16之间存在细微差别。 UTF-16取代了UCS-2。你可以在这里阅读有关的差异:

http://en.wikipedia.org/wiki/UTF-16/UCS-2

#2


0  

I couldn't get this to work.

我无法让这个工作。

I has a html string with   NON BREAKING SPACE

我有一个带有 的html字符串没有突破空间

Great Guildford St/SouthwarkSt & nbsp;Stop:& nbsp; BM
Walk to SE1 0HL
"Great Guildford St/SouthwarkSt \U00a0Stop:\U00a0 BM",

I tried 3 types of encode/decode

我尝试了3种编码/解码

// NSData   *asciiData   = [instruction dataUsingEncoding:NSUTF16BigEndianStringEncoding];                                          
// NSString *asciiString = [[NSString alloc] initWithData:asciiData 
//     encoding:NSUTF16BigEndianStringEncoding];

// NSData   *asciiData   = [instruction dataUsingEncoding:NSASCIIStringEncoding];                                           
// NSString *asciiString = [[NSString alloc] initWithData:asciiData 
//     encoding:NSASCIIStringEncoding];

//little endian
NSData   *asciiData   = [instruction dataUsingEncoding:NSUTF16LittleEndianStringEncoding];                                          
NSString *asciiString = [[NSString alloc] initWithData:asciiData
    encoding:NSUTF16LittleEndianStringEncoding];

none of these worked. They seemed to work as if I NSLog the string it looks ok

这些都没有奏效。他们似乎工作好像我NSLog它看起来不错的字符串

NSLog(@"HAS UNICODE  :%@", instruction); 
..do encode/decode
NSLog(@"UNICODE AFTER:%@", asciiString);

Which output

HAS UNICODE: St/SouthwarkSt  Stop:  BM
UNICODE AFTER: St/SouthwarkSt  Stop:  BM

but I happened to store these in an NSArray and I happened to call [stringArray description] and all the unicode was still in there

但我碰巧将这些存储在NSArray中,我碰巧调用[stringArray description]并且所有的unicode仍然在那里

instructionsArrayString: (
    "Great Guildford St/SouthwarkSt \U00a0Stop:\U00a0 BM",
    "Walk to SE1 0HL"
)

So something in NSLog hides   but it shows up in NSArray description so you may think youve removed the Unicode when you haven't.

所以NSLog中的东西隐藏了 但它出现在NSArray描述中,所以你可能认为你没有删除Unicode。

Will try another method that replace the characters.

将尝试另一种替换字符的方法。


推荐阅读
author-avatar
越野瘾君子_939
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有