Encode Arabic unichar to regular letters

please help me encode the unichar arabic letters I have in my Alphabet.xml file to regular arabic letters. For example: encode 0x0628 in regular Arabic ب (B). I 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 do NSLog

, it prints Chinese or blah blah blah characters that I have never seen before. But when I just put the char encoding into the unicode encoding below it converts to a regular Arabic letter. Please help !! :(

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

      

0


source to share


1 answer


The problem is your code:

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

      

equivalent to:

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

      

but stringWithFormat

expects an integer 0x0628

, not a string @"0x0628"

.




So, you need to convert charcode

before using it. You can achieve this using the following code:

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

      

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

NSLog(@"%C", charValue);

      

+1


source







All Articles