ScrumMasterMind
ScrumMasterMind

Reputation: 71

How do I do I convert Arabic (Unicode encoded) to byte hex representation in Objective-C?

I am trying to use http request to calling a website using iPhone; by developing a little application in Objective-C, however I am having problems passing Arabic characters! as I need to use unicode in hex like (Arabic characters in Unicode ?

I have done this before developing a website using PHP using the below function:

/**
* Converts a string to USC-2 encoding if neccessary.
*/

function string_unicode($string) 
{
    if (function_exists('iconv')) 
    {  
      $latin = @iconv('UTF-8', 'ISO-8859-1', $string);

        if (strcmp($latin, $string)) 
         {
            $arr = unpack('H*hex', @iconv('UTF-8', 'UCS-2BE', $string));
              return strtoupper($arr['hex']) .'&unicode=1';  
         }
     }

    return FALSE;
 }

BUT I am struggling to achieve the same in Objective-C, please help!! Thanks

Upvotes: 2

Views: 1384

Answers (1)

ScrumMasterMind
ScrumMasterMind

Reputation: 71

Finally it was rather very simple solution by using formatting!!

-(NSString *)getUniCode:(NSString *)aString
{
    NSString *theStringToReturn  = nil;


    NSMutableString *theEncodedString = [NSMutableString stringWithString:@""];

    for (NSInteger theCharIndex=0; theCharIndex< [message length]; theCharIndex++) {

        [theEncodedString appendFormat: @"%04x", [message characterAtIndex: theCharIndex]];

    }

    theStringToReturn = [NSString stringWithString:theEncodedString];

    return theStringToReturn;
}

I hope this will help others.

Upvotes: 3

Related Questions