Arabic symbols

I have some problems displaying some Arabic text in an iPhone app. When I show it to UILabel

, it displays as ?????? ????? ??????

. The string comes from the server as XML, I parse it and render it to UILabel. I don't know if this is related to an issue on the iPhone or from the server side.

  • I want to know how to determine that the string coming from the server has the correct encoding and is a valid Arabic character.

  • Is it enough to print the meaning of each character and check if it is between the Arabic Unicode characters. (As ASCII, the value of the A character is 65 and the Z value is 90. Thus, the value 70 must be an ASCII character).

  • In the server, the string is encoded with UTF8 encoding. And a server program written in C #. What is the post encoding method to translate Arabic text from server to iPhone?

  • Do I need to use other fonts to display Arabic characters correctly.

  • Is there any Arabic content XML file on the Internet from where I can parse and display the Arabic text correctly?

Thanks in advance.


When I NSLogged the XML data, I had the same symbols ???? ???? ???



See the XML data I got in the console.

<SCA_LONG_NAME>???? ????? ?????????? ????? ?????????</SCA_LONG_NAME>
<SCA_SHORT_NAME>???? ????? ?????????</SCA_SHORT_NAME>



source to share

4 answers

Thanks for all the answers. The iPhone isn't the problem. The XML server was not properly encoded to UTF8 string. When correctly coded, I got a valid Arabic character. I just did the code like

  NSString* lableText = [[NSString alloc] initWithData:serverSentData encoding: NSUTF8StringEncoding ];


to convert NSData to NSString and it worked well.



You must send and receive text from the server as UTF-8 !!

From NSString.h:

enum {
    NSASCIIStringEncoding = 1,      /* 0..127 only */

    **NSUTF8StringEncoding = 4,**

    **NSUTF16StringEncoding = NSUnicodeStringEncoding,**      /* An alias for NSUnicodeStringEncoding */  
typedef NSUInteger NSStringEncoding;


The iOS application code should look like this: ((NSData *) serverSentData is the nsdata representation of the string received from the server)

NSString* lableText = [[NSString alloc] initWithData:serverSentData encoding:NSUTF8StringEncoding ];


if it's UTF-16:

NSString* lableText = [[NSString alloc] initWithData:serverSentData encoding:NSUTF16StringEncoding ];


By the way, you can use json. This worked well for me when I was sending cyrillic text.



If your server is sending encoding in the header:

<?xml version="1.0" encoding="UTF-8" ?>


then the built-in or any other well-written XML parser will process and decode the PCDATA (text between tags) in the correct way. If your server is not sending this information and you cannot change it on the server side, then you need to guess what encoding the server is using and convert the result manually to UTF-8. For example, if your string is in pure 16-bit Unicode, you can do it like this:

NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUnicodeStringEncoding] autorelease];
NSData* convertedData = [responseString dataUsingEncoding:NSUTF8StringEncoding];






All Articles