1

In my application, I am getting some string values from a server, but I'm not ending up with the right string.

بسيط this is the string from server side, but what I am getting is Ø¨Ø³ÙØ·

I tried to test the response string in an online decoder:

http://www.cafewebmaster.com/online_tools/utf8_encode

It is UTF-8 encoded, but I couldn't decode the string on the iPhone side.

I took a look at these Stack Overflow links as reference

Converting escaped UTF8 characters back to their original form
unicode escapes in objective-c
utf8_decode for objective-c

but none of them helped.

Community
  • 1
  • 1
Raj
  • 5,895
  • 4
  • 27
  • 48

3 Answers3

1

I don't understand from your question the following points:

  1. Do you have access on the server side (I mean the programming of it)?
  2. How do you send and receive data to the server?

For the first question I will assume that the server is programmed to send you text in UTF-8 encoding.

Now on the iPhone if you are sending to the server using sockets use the following:

NSString *messageToSend = @"The text in the language you like";
const uint8_t *str = (uint8_t *) [messageToSend cStringUsingEncoding:NSUTF8StringEncoding];
[self writeToServer:str];

Where the function writeToServer is your function that will send the data to the server.

If you are willing to put the data in a SQLite3 database use:

sqlite3_bind_text(statement, 2, [@"The text in the language you like" UTF8String], -1, NULL);

If you are receiving the data from the server (again using sockets) do the following:

[rowData appendBytes:(const void *)buf length:len];
NSString *strRowData = [[NSString alloc] initWithData:rowData encoding:NSUTF8StringEncoding];

I hope this covers all the cases you need.

antf
  • 3,162
  • 2
  • 26
  • 33
0

Without any source it is hard to say anything conclusive, but at some point you are interpreting a UTF-8 encoded string as ISO-8859-1, and (wrongfully) converting it to UTF-8:

Analysis for string 'بسيط':

  • raw length: 8
  • logical length: 4
  • raw bytes: 0xD8 0xA8 0xD8 0xB3 0xD9 0x8A 0xD8 0xB7
  • interpreted as ISO-8859-1 (Ø¨Ø³ÙØ·): 0xC3 0x98 0xC2 0xA8 0xC3 0x98 0xC2 0xB3 0xC3 0x99 0xC2 0x8A 0xC3 0x98 0xC2 0xB7

So at some point you should probably find some reference to ISO-8859-1 in your code. Find it and remove it.

Community
  • 1
  • 1
mvds
  • 45,755
  • 8
  • 102
  • 111
0

SOLVED the issue from this link

Different kind of UTF8 decoding in NSString

NSString *string = @"Ø¨Ø³ÙØ·";

I tried

[NSString stringWithUTF8String:(char*)[string cStringUsingEncoding:NSISOLatin1StringEncoding]]

this method

Thank You.

Community
  • 1
  • 1
Raj
  • 5,895
  • 4
  • 27
  • 48
  • 1
    This doesn't sound like you've actually solved something. I hope you don't get paid for "solutions" like this. – mvds Feb 19 '12 at 20:41
  • @mvds See i can solve my problem by the above method, i posted this here for helping others finding same solutions , may be it will helpful for others – Raj Feb 19 '12 at 21:39
  • @mvds whats wrong with this , utf8 encoded string can be decoded by using the above method.And i clearly mentioned that the solution solved my issue , So you tell me what would be the right answer for this question?...i ddnt find any thing helpful in your answer !!!!!!!sorry – Raj Feb 20 '12 at 08:50
  • I've clearly stated what's wrong with it. The most obvious thing is the magic number **5**. Please explain to me what the number 5 is doing there. You have a bug somewhere in your code (reading in UTF8 as if it were ISO-8859-1) and you have not fixed it, but put in the "reverse" bug to work around it. – mvds Feb 20 '12 at 09:25
  • 5 means NSISOLatin1StringEncoding – Raj Feb 21 '12 at 09:25
  • Ok, lets start by putting `NSISOLatin1StringEncoding` in there instead of the number 5. Not everyone in the world knows that. – mvds Feb 21 '12 at 09:34