在 iPhone 上检测 NSString 中的 Unicode 字符

发布于 2024-08-11 08:49:32 字数 270 浏览 3 评论 0原文

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

我需要这样做是因为 unicode 字符在消息中占用更多空间,而且还因为我需要将它们转换为相应的十六进制字符。

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

例如繁=7E41,体=4F53,中=4E2D,文=6587

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.

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.

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?

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

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

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

发布评论

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

评论(2

奶气 2024-08-18 08:49:32

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

[myString canBeConvertedToEncoding:NSASCIIStringEncoding];

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

[myString dataUsingEncoding:NSUTF16BigEndianStringEncoding];

或 NSUTF16LittleEndianStringEncoding,具体取决于您的平台。 UCS-2 和 UTF-16 之间存在细微差别。 UTF-16 已取代 UCS-2。您可以在此处了解差异:

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

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

[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:

[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:

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

硬不硬你别怂 2024-08-18 08:49:32

我无法让它发挥作用。

我有一个 html 字符串,其中包含   NON BREAKING SPACE

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

我尝试了 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];

但这些都不起作用。
它们似乎工作得好像我 NSLog 字符串它看起来不错

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

哪个输出

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

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

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

所以有些东西在 NSLog 中隐藏   但它显示在 NSArray 描述中,因此您可能认为您已经删除了 Unicode,但实际上并没有。

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

I couldn't get this to work.

I has a html string with   NON BREAKING SPACE

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

I tried 3 types of encode/decode

// 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(@"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

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.

Will try another method that replace the characters.

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