简体   繁体   English

stringWithCString:encoding:当输入的cstring包含表情符号时,返回nil

[英]stringWithCString:encoding: returned nil, when the input cstring contains emoji

Here is the story. 这是故事。 I'm using a C lib for data transmission, and all work well except for certain Emoji. 我正在使用C库进行数据传输,除某些表情符号外,其他所有功能都运行良好。 For the receiver, the received message is char *message , for which can use " printf " to print out the right thing. 对于接收者,接收到的消息为char * message ,可以使用“ printf ”来打印出正确的内容。 but when using stringWithCString:encoding: (or stringWithUTF8String: etc.), it return nil for certain emoji. 但是当使用stringWithCString:encoding:(stringWithUTF8String:等)时,它为某些表情符号返回nil。

So, I print out each char, and here the successful emoji: 因此,我打印出每个字符,下面是成功的表情符号:

"\\xe2\\x9a\\xbd" “\\ XE2 \\ x9a \\ XBD”

"\\xe2\\x9a\\xbe\\xef\\xb8\\x8f" “\\ XE2 \\ x9a \\ XBE \\ XEF \\ XB8 \\ X8F”

"\\xe2\\x98\\x81\\xef\\xb8\\x8f" “\\ XE2 \\ X98 \\ X81 \\ XEF \\ XB8 \\ X8F”

But the failed emoji: 但是失败的表情符号:

"\\xed\\xa0\\xbc\\xed\\xbe\\x82" “\\固定的\\ XA0 \\命苦\\固定的\\ XBE \\ X82”

"\\xed\\xa0\\xbc\\xed\\xbf\\x80" “\\固定的\\ XA0 \\命苦\\固定的\\ XBF \\ X80”

"\\xed\\xa0\\xbc\\xed\\xbc\\x88" “\\固定的\\ XA0 \\命苦\\固定的\\命苦\\ X88”

have beening stuck on this days. 这几天一直被困住。

any ideas? 有任何想法吗?

Thanks so much!! 非常感谢!!

It's because your bytes are not a valid UTF-8 encoded string. 这是因为您的字节不是有效的UTF-8编码的字符串。 According to UTF-8 , "\\xed\\xa0\\xbc" encodes the Unicode code point U+D83C. 根据UTF-8 ,“ \\ xed \\ xa0 \\ xbc”对Unicode代码点U + D83C进行编码。 This is a code point in the high surrogate block. 这是高代理块中的代码点。 Surrogates are not valid characters and are reserved for UTF-16 purposes. 替代字符不是有效字符,并且保留用于UTF-16。 It is not valid for UTF-8 to encode such codepoints. UTF-8编码此类代码点无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSString stringWithCString:encoding:-不复制Cstring吗? - NSString stringWithCString:encoding: - not copying the Cstring? [NSString stringWithCString:变量编码:1]中的编码是什么; 或stringWithUTF8String - what is encoding in [NSString stringWithCString: variable encoding:1]; or stringWithUTF8String 文本包含表情符号时文本高度错误 - Wrong text height when text contains emoji 不推荐使用stringWithCString ...那么我对sysctlbyname返回的char *使用什么? - stringWithCString deprecated… so what do i use with char* returned by sysctlbyname? 字符串编码和表情符号出现问题 - Trouble with string encoding and emoji 使用CLGeocoder的ResreverseGeocodeLocation但返回的地标为nil且kCLErrorDomain错误= 8时 - When use ResreverseGeocodeLocation of CLGeocoder but the returned placemarks is nil and the kCLErrorDomain error =8 Swift Array 将 nil 的 var 传递给 .contains 和 .filter 时会发生什么 - Swift Array what happens when passing var that is nil to .contains and .filter 如何将旧的表情符号编码转换为iOS5中的最新编码? - how to convert the old emoji encoding to the latest encoding in iOS5? 从dateFromString NSDateFormatter返回的nil - nil returned from dateFromString NSDateFormatter NSData编码为Unicode返回nil - NSData encoding to Unicode returning nil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM