• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
endian problems with UTF16 on Intel Macs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

endian problems with UTF16 on Intel Macs


  • Subject: endian problems with UTF16 on Intel Macs
  • From: Donald Hall <email@hidden>
  • Date: Mon, 28 Aug 2006 23:47:43 -0600

I have run into an endian problem with Intel Macs that I don't know how to solve despite some good hints in the archives and documentation. I have an AppleScript application that is feeding UTF16 text into a log file. A second Cocoa based app reads the file and displays it in a window. Everything looks okay until I make a change to the log file in the Cocoa app and resave the file. Any new data added to the log file by the AppleScript application after the save is obviously byte reversed as it comes out in Oriental characters. Saving the file must change the endian-ness of the data to Intel native format.

I understood from reading the documentation that pairing the functions CFStringCreateExternalRepresentation and CFStringCreateFromExternalRepresentation would keep the stored data in "external representation" rather than platform native "internal representation". Furthermore, I understood that "external representation" was always big endian. (Note that this problem does not occur on PowerPC Macs.) I therefore tried to use these functions for saving and loading my log data as the Cocoa equivalents were not giving the correct result.

Here is the code I am using to load and save data from and to the log file in my NSDocument subclass:

To save the string:

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
[self updateString]; // updates the ivar 'string'
// the following Cocoa method didn't work, so I tried CF methods
//return [string dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];


CFDataRef myDataRef = CFStringCreateExternalRepresentation (kCFAllocatorDefault, (CFStringRef)string, kCFStringEncodingUnicode,'?');
return (NSData *)myDataRef;
}


To load the data and re-establish the 'string' ivar:

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
CFStringRef myCFString;
myCFString = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault,
(CFDataRef) data, kCFStringEncodingUnicode);
NSString* aString = (NSString*)myCFString;
// the following Cocoa method didn't work, so I tried CF methods
//NSString *aString = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];
[self setString:aString];
[aString release];
[self updateView]; // redisplays the string in the document window
return YES;
}


(And yes, I know these methods are deprecated in Tiger. The Tiger methods are there and call the ones above. These are left for Panther compatibility.)

Can anyone point me in the right direction? How to I keep the Unicode data big endian when saving from the Cocoa application to be consistent with data added to the file by the AppleScript application? Do I have to manually insert a BOM into my string before saving? Swap bytes before returning the NSData object?

Thanks,

Don
--
Donald S. Hall, Ph.D.
Apps & More Software Design, Inc.
email@hidden
http://www.appsandmore.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: endian problems with UTF16 on Intel Macs
      • From: Chris Suter <email@hidden>
  • Prev by Date: Re: NSTableColumn subclass and custom binding (solution!)
  • Next by Date: Re: endian problems with UTF16 on Intel Macs
  • Previous by thread: Re: NSTableColumn subclass and custom binding (solution!)
  • Next by thread: Re: endian problems with UTF16 on Intel Macs
  • Index(es):
    • Date
    • Thread