因为和硬件交互需要传递16进制的emoje并且显示
试过以下方式
unichar ch = [str characterAtIndex:0];
发现有规则限制(不能大于\ UFFFF的Unicode值)
后自己写以下方法单独处理
- (void)printTest{
NSString * inputString = @"🌞:";
NSString *uniStr = [NSString stringWithUTF8String:[inputString UTF8String]];
NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodStr = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding] ;
NSLog(@"%@",goodStr);
[self coverteStingToArray:inputString completed:^(NSArray *array) {
for(int i = 0 ; i < array.count; i ++){
NSString *byteString = [array objectAtIndex:i];
//取UTF-32编码
NSData *data = [byteString dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
uint32_t unicode;
[data getBytes:&unicode length:sizeof(unicode)];
NSLog(@"%x", unicode);
}
}];
}
//逐个取出字符
- (void)coverteStingToArray:(NSString *)inputString completed:(void (^)(NSArray *array))completeBlock{
NSMutableArray *returnArray = [NSMutableArray new];
NSRange fullRange = NSMakeRange(0, inputString.length);
[inputString enumerateSubstringsInRange:fullRange
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
[returnArray addObject:substring];
if(substringRange.location + substringRange.length == inputString.length){
completeBlock(returnArray);
}
}];
}