Arabic characters issue

2020-05-06 16:36发布

问题:

I have some problems with displaying some Arabic text in my iPhone application. When I am displaying it on a UILabel, it shows like ?????? ????? ?????? The string comes from server as XML, I parse it and display on a UILabel. I dont know this is due to a problem in iPhone or from Server side.

  • I want to know how can I determine the string coming from server is having correct encoding and is a valid Arabic characters.

  • Is it suffice to print value each character and check it lies between Arabic Unicode characters. (Like ASCII value of character A is 65 and that of Z is 90. So a value 70 must be a ASCII character).

  • In server the string is encoded with UTF8 encoding. And server program written in c#. What is the write encoding method to transfer Arabic text from server to iPhone?

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

  • Is there any XML file with Arabic content in Internet, from where I can parse and display arabic text correctly?

Thanks in advance..

EDIT:

When I NSLogged XML data I got same ???? ???? ??? characters.

EDIT

See a XML styled data that I got in 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>

回答1:

You should sent to and receive text from server both in UTF-8 !!

From NSString.h:

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

    **NSUTF8StringEncoding = 4,**
    ...

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

In your iOS app code should look like following: ((NSData*)serverSentData - nsdata representation of string recieved from 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. It worked for me fine when I was sending cyrylic text.



回答2:

Thanks for all answers. The problem was not with iPhone side. In server the XML was not correctly encoded to UTF8 string. When it correctly encoded I got valid arabic character. I only did code like

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

for converting NSData to NSString and it worked well!!!



回答3:

If your server sends the encoding in the header:

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

then the built-in or any other well-written XML parser will handle and decode the PCDATA (the text between tags) in the correct way. If your server does not send this info and you can't change it on server side, then you have to guess yourself which 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 this way:

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


回答4:

This post may help:

encode arabic letter unichar to regular letters