iOS转换十六进制值 [英] iOS Convert Hex value

查看:235
本文介绍了iOS转换十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to convert hexadecimal value into emoji icons , I have a string like below

NSString *myVal = @"1F61E";

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

how can i convert this text to display it as emoji charrcaters?

我发现价值来自此链接
请让我知道,我对此问题非常不满

I have found that value from this link Please let me know, i am really stuck-up with this issue

已更新

Updated

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编码:

The code point that you are trying to encode does not fit in 16 bits. Therefore you need to use UTF-32 encoding:

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);

这篇关于iOS转换十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆