Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i'm trying to convert the contents of an XML file (that is encoded in UTF-8) into data... everything works fine if the characters of the file are english, but this code seems to be double encoding if the characters of the file are in anything but (IE Japanease or french, etc.)

Code:
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
	{
   	NSPasteboard *pboard = [sender draggingPasteboard];
	NSString *dropedFilePath = [[pboard propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
	NSString *droppedFileContents = [NSString stringWithContentsOfFile:dropedFilePath];
		
	AquaticPrime *licenseValidator = [AquaticPrime aquaticPrimeWithKey:key];
	NSData *licenseData = [droppedFileContents [B]dataUsingEncoding:NSUnicodeStringEncoding[/B]];
	NSDictionary *licenseDictionary = [licenseValidator dictionaryForLicenseData:licenseData];

for for example... i will input this XML file into my app, the NSLog returns unexpected results:


Input:
<key>Name</key>
<string>槇原敬之</string>


Output:
<key>Name</key>
<string>ÊßáÂéüÊ降πã</string>



what should i do?
 

kpua

macrumors 6502
Jul 25, 2006
294
0
First of all, +stringWithContentsOfFile is deprecated because it is a string creation method from bytes that doesn't specify encoding. Use +stringWithContentsOfFile:encoding:error: instead and to specify the correct encoding when you read it from disk into an NSString. That will eliminate the "double encoding" problem you're seeing.

Second of all, if your string is encoded with UTF-8, why aren't you using NSUTF8StringEncoding?

Finally, why are you converting to a string then back to a data? Why not just read in the file with +dataWithContentsOfFile:?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
For the purposes of this particular use... create an NSData object directly from the file rather than a string. It saves you from having to do any magic yourself.

If you were doing the conversion yourself anyways, you should probably be using NSUTF8Encoding rather than NSUnicodeEncoding. Otherwise the XML parser will choke as you see, since the text is 16-bit unicode, but the XML says it is UTF-8.

Again, the easiest way is to just use the NSData method to create a data object from a file at the path, and pass than into AquaticPrime.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.