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.

EDIT:

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

.

EDIT

See the XML data I got in the console.

<CB_SEC_COMP>
<SC_COMP_ID>9999</SC_COMP_ID>
<SCA_LONG_NAME>???? ????? ?????????? ????? ?????????</SCA_LONG_NAME>
<SCE_LONG_NAME>CHINA SECURITY &amp; SURVEILLANCE TECHNOLOGY, INC.</SCE_LONG_NAME>
<SCA_SHORT_NAME>???? ????? ?????????</SCA_SHORT_NAME>
<SCE_SHORT_NAME>CHINA SECURITY &amp; SUR</SCE_SHORT_NAME>
<SC_MRK_CODE>9</SC_MRK_CODE>
<SC_SEC_CODE>86</SC_SEC_CODE>
<SC_STATUS>Y</SC_STATUS>
<TICKER_ID>CSR</TICKER_ID>
<SC_MRK_TYPE_CODE>0001</SC_MRK_TYPE_CODE>
<SC_EXCHANGE>DFM</SC_EXCHANGE>
<CUR_CODE>AED</CUR_CODE>
</CB_SEC_COMP>

      

0


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.

+1


source


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.

+4


source


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

      

0


source


0


source







All Articles