Skip navigation.
 
mlRe: From Big Endian to Little Endian
FROM : Adam R. Maxwell
DATE : Fri Aug 11 22:36:57 2006

On Aug 11, 2006, at 12:54, Julio Cesar Silva dos Santos wrote:

> Hi, a couple of months ago I sent a message to the list explaining 
> a problem with strings returned from iTunes and Intel Macs. My 
> solution was to create a CFString using CFStringCreateWithBytes. 
> (see http://www.cocoabuilder.com/archive/message/cocoa/
> 2006/6/27/166393)
> Now a user that is using Panther asked me that when he tries to 
> save strings back to iTunes, they are saved as chinese characters. 
> This happened because I had to change the code to check if the 
> program is running on Tiger or Panther.
> When using Tiger the code looks like this (value is an NSString):
>
> len = [value lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];


This should be the same as [value length].

> err = AEBuildDesc(&valueDesc, NULL, "'utxt'(@)", len,  [value 
> cStringUsingEncoding:NSUnicodeStringEncoding]);
>
> When using Panther I tried this:
>
> len = [value cStringLength];


This is the length in the default C string encoding (deprecated)...

> err = AEBuildDesc(&valueDesc, NULL, "'utxt'(@)", len,  [value 
> UTF8String]);


You're using a mismatched length (default C encoding), data (UTF-8) 
and type (UTF-16 native) here.  Try this:

NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
len = [data length];
if(data)
  err = AEBuildDesc(&valueDesc, NULL, "'utf8'(@", len, [data bytes]);

which should work regardless of endianness on Tiger and Panther. 
This code uses typeUTF8Text instead of 'utxt' (which is also 
deprecated according to AEDataModel.h).

-- Adam

Related mailsAuthorDate
mlFrom Big Endian to Little Endian Julio Cesar Silva… Aug 11, 21:54
mlRe: From Big Endian to Little Endian Adam R. Maxwell Aug 11, 22:36