Converting String To Data Without Encoding?

Discussion in 'Mac Programming' started by Darkroom, Oct 13, 2008.

  1. Darkroom Guest


    Dec 15, 2006
    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.)

    - (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:



    what should i do?
  2. kpua macrumors 6502

    Jul 25, 2006
    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:?
  3. Krevnik macrumors 68030


    Sep 8, 2003
    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.

Share This Page