Convert unicode

前端 未结 3 1970
眼角桃花
眼角桃花 2021-01-15 12:48

I have a UITextField to enter a unicode value , when i tap a UIButton need to convert it and showing in a UILabel.
Th

相关标签:
3条回答
  • 2021-01-15 13:20

    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.
    }
    

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

    0 讨论(0)
  • 2021-01-15 13:23

    Try changing the font of your UILable.
    Actually some fonts can't display all unicode outputs!
    This happened to me in Java Swing once!
    I was trying to display a unicode string on a JLabel but the unicode string wasn't being displayed on JLablel.
    Then I changed the font to Arial the unicode values got displayed!
    So I'd suggest you to try changing the font from to Arial or some other font.

    Hope this helps!

    0 讨论(0)
  • 2021-01-15 13:39

    Problem is input should be char pointer

    NSString *str = [NSString stringWithUTF8String:[self.textField.text UTF8String]];
        m_CtrlLabel.text=str;  
    
    0 讨论(0)
提交回复
热议问题