转换unicode [英] Convert unicode

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

问题描述

我有一个UITextField输入unicode值,当我点击UIButton时需要将其转换并显示在UILabel中.
下面的代码对我来说很好(我代码中的unicode):

I have a UITextField to enter a unicode value , when i tap a UIButton need to convert it and showing in a UILabel.
The below code is working fine for me(unicode inside my code):

NSString *str = [NSString stringWithUTF8String:"\u0D05"];
    m_CtrlLabel.text=str;  

我的问题是我无法从UITextField转换4位unicode.那就是我在UITextField内键入0D05,我需要将其转换并显示在标签中,我尝试了很多组合,但是没有运气.
预先感谢

My problem is I can't convert the 4 digit unicode from the UITextField. That is I am typing 0D05 inside the UITextField , I need to convert it and show in the label , I have tried a lot of combinations but no luck.
Thanks in advance

推荐答案

0D05只是一个十六进制数.您可以使用NSScanner将十六进制字符串解析为整数,然后创建一个包含Unicode字符的NSString.

0D05 is just a hexadecimal number. You can use NSScanner to parse the hexadecimal string into an integer, and then create a NSString containing the Unicode character.

NSString *hexString = yourInputField.text;

NSScanner *scanner = [NSScanner scannerWithString:hexString];
uint32_t unicodeInt;
if ([scanner scanHexInt:&unicodeInt]) {
    unicodeInt = OSSwapHostToLittleInt32(unicodeInt); // To make it byte-order safe
    NSString *unicodeString = [[NSString alloc] initWithBytes:&unicodeInt length:4 encoding:NSUTF32LittleEndianStringEncoding];
    yourOutputLabel.text = unicodeString;
} else {
    // Conversion failed, invalid input.
}

即使在Unicodes> U + FFFF(例如1F34C)下也可以使用(感谢R. Martinho Fernandes的反馈).

This works even with Unicodes > U+FFFF, such as 1F34C (thanks to R. Martinho Fernandes for his feedback).

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

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