作者:越野瘾君子_939 | 来源:互联网 | 2023-08-14 12:26
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 个解决方案
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.
将尝试另一种替换字符的方法。