Melbourne
Melbourne

Reputation: 541

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

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

Upvotes: 2

Views: 977

Answers (3)

S1LENT WARRIOR
S1LENT WARRIOR

Reputation: 12214

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!

Upvotes: 0

Martin R
Martin R

Reputation: 540105

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

Upvotes: 4

Hasintha Janka
Hasintha Janka

Reputation: 1639

Problem is input should be char pointer

NSString *str = [NSString stringWithUTF8String:[self.textField.text UTF8String]];
    m_CtrlLabel.text=str;  

Upvotes: 0

Related Questions