rakeshNS
rakeshNS

Reputation: 4257

Arabic characters issue

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.

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>

Upvotes: 0

Views: 4390

Answers (3)

rakeshNS
rakeshNS

Reputation: 4257

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!!!

Upvotes: 1

MrTJ
MrTJ

Reputation: 13202

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

Upvotes: 0

velkopopovizky
velkopopovizky

Reputation: 148

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.

Upvotes: 4

Related Questions