[英]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.