iphone - iOS 转换十六进制值

标签 iphone objective-c ios ios4

如何将十六进制值转换成表情符号图标,我有一个如下所示的字符串

NSString *myVal = @"1F61E";

我如何转换此文本以将其显示为表情符号字符?

我找到了那个值 from this link 请让我知道,我真的很困惑这个问题

已更新

NSString *utf8String1 = @"1F61E";
NSString *a = [self convert:utf8String1];
NSLog(@"%@ &&&&&&&&&&&&&&&&&&&&&",a);


-(NSString*)convert:(NSString*)decoded{

    unichar unicodeValue = (unichar) strtol([decoded UTF8String], NULL, 16);
    char buffer[2];
    int len = 1;

    if (unicodeValue > 127) {
        buffer[0] = (unicodeValue >> 8) & (1 << 8) - 1;
        buffer[1] = unicodeValue & (1 << 8) - 1; 
        len = 2;
    } else {
        buffer[0] = unicodeValue;
    }

    return [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];



}

最佳答案

您尝试编码的代码点不适合 16 位。因此你需要使用 UTF-32 编码:

NSScanner *scan = [[NSScanner alloc] initWithString:@"1F61E"];
unsigned int val;
[scan scanHexInt:&val];
char cc[4];
cc[3] = (val >> 0) & 0xFF;
cc[2] = (val >> 8) & 0xFF;
cc[1] = (val >> 16) & 0xFF;
cc[0] = (val >> 24) & 0xFF;
NSString *s = [[NSString alloc]
    initWithBytes:cc
           length:4
         encoding:NSUTF32StringEncoding];
NSLog(@"[%@]", s);

关于iphone - iOS 转换十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9888456/

相关文章:

iphone - NSCache崩溃的应用程序

ios - 无法将通用值分配给扩展中的协议(protocol) associatedType 变量

objective-c - 将 Objective-C 指针类型 'NSString *' 转换为 C 指针类型 'CFStringRef' (又名 'const struct __CFString *' )需要桥接转换

ios - 将 iOS 更新到 7.1 后,Sprite-kit 游戏崩溃

ios - 如何获得时间(下午 1 - 2 点)或(下午 2 - 3 点)

ios - 转到另一个 View Controller TableView 单元格 xib

javascript - 如何在 iOS 上的移动网络浏览器中使键盘触发调整大小事件?

iPhone:通过给定的 CGPath 剪辑用户提供的 UIImage

ios - 如何为 iOS (iPhone) 应用程序配置十进制数字字段?

iphone - 使用 AUGraphs 进行麦克风输入