user1268773
user1268773

Reputation:

encode arabic letter unichar to regular letters

please help me to encode the unichar arabic letters that i have in my Alphabet.xml file to regular arabic letters. For example: encode 0x0628 to regular arabic ب (B). I have used:

    -(void)loadDataFromXML{

    NSString* path = [[NSBundle mainBundle] pathForResource: @"alphabet" ofType: @"xml"];
    NSData* data = [NSData dataWithContentsOfFile: path];
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];
    [parser setDelegate:self];
    [parser parse];
    [parser release];    
    }



    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{

 if ([elementName isEqualToString:@"letter"]) {
       NSString* title = [attributeDict valueForKey:@"name"];     
       NSString * charcode  =[attributeDict valueForKey:@"charcode"];
       NSLog(@"%@",[NSString stringWithFormat:@"%C",charcode]);

      }
    }

When i NSLog, it prints Chinese or something blah blah blah symbols that i never seen before. But when i just put the unicode encoded char ilk below it convert to normal arabic letter. Please help!! :(

 NSLog(@"%@",[NSString stringWithFormat:@"%C",0x0628]);

Upvotes: 0

Views: 1146

Answers (1)

sch
sch

Reputation: 27506

The problem is that your code:

NSLog(@"%@",[NSString stringWithFormat:@"%C", charcode]);

is equivalent to:

NSLog(@"%@",[NSString stringWithFormat:@"%C", @"0x0628"]);

but stringWithFormat is expecting an integer 0x0628 and not a string @"0x0628".


So you have to convert charcode before using it. You can achieve that using the following code:

uint charValue;
[[NSScanner scannerWithString:charcode] scanHexInt:&charValue];
NSLog(@"%@",[NSString stringWithFormat:@"%C", charValue]);

Finally, note that you can simply log the character without creating an intermediate string using:

NSLog(@"%C", charValue);

Upvotes: 1

Related Questions