The fffe
at the start is a byte order mark that indicates that the response is is little endian UTF-16. (See this byte order mark table.) This is further confirmed if you look at the rest of your data, where 3c00
is the little endian UTF-16 representation of <
, 3f00
is ?
, 7800
is x
, etc.
Thus:
fffe3c00 3f007800 6d006c00 20007600 65007200 73006900 6f006e00 3d002200 31002e00 30002200 20006500 6e006300 6f006400 69006e00 67003d00 22007700 69006e00 64006f00 77007300 2d003100 32003500 32002200 20007300 74006100 6e006400 61006c00 6f006e00 65003d00 22007900 65007300 22003f00 3e00
is
<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
You should therefore convert it to a string using:
NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF16LittleEndianStringEncoding];
This will automatically handle the byte order mark, as well (eliminating that curious ÿþ
at the start).
By the way, if you're ever unclear about the representation, you can always try saving the NSData
to a file and then using stringWithContentsOfFile:usedEncoding:error:
. Personally, I'd always first look for a the byte order mark or for obvious UTF-16 or UTF-32 data (which is generally pretty easy to identify in western languages), as we did above, but this can be useful sometimes:
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:&error];
if (string) {
NSLog(@"string = %@", string);
NSLog(@"encoding = %d", encoding);
} else {
NSLog(@"stringWithContentsOfFile error: %@", error);
}
It doesn't always work, but sometimes it provides interesting clues.