Subject: Re: NSString array to -> char**
Subject: Re: NSString array to -> char**
- Subject: Subject: Re: NSString array to -> char**
- From: Brant Vasilieff <email@hidden>
- Date: Thu, 20 Mar 2003 12:21:13 -0800
-(NSMutableData*)
arrayOfCStringsWithEncoding:(NSStringEncoding)encoding {
NSRange r= NSMakeRange(0, sizeof(char*));
NSData* tempData;
char zero = 0;
try using an UniChar here, incase your encoding calls for two bytes
support. So there will be an extra byte between strings, but you'll be
safer.
int n = [self count];
NSMutableData *data = [NSMutableData dataWithLength:
(n+1)*sizeof(char*)];
NSEnumerator* e = [self objectEnumerator];
NSString* s;
int tempLength;
while (s = [e nextObject]) {
tempData = [s dataUsingEncoding:encoding
allowLossyConversion:YES];
[data appendData:tempData];
[data appendBytes: &zero length: sizeof(char)];
tempLength = [tempData length];
[data replaceBytesInRange: r withBytes: (void*)&tempLength
length: r.length];
r.location += r.length;
}
char** p = [data mutableBytes];
char* firstString = (char*)p + (n+1)*sizeof(char*);
while (n--) {
int length = *(int*)p;
//// Is this right? the first string is at firstString,
//// and the second should start at firstString + length
//// of first string, no?
*p = firstString + length;
So you're storing the address to the termination character for your
firstString. Remember FirstString already points to the beginning of
the string, and you're adding the length;
How about changing the variable name from firstString to stringPtr and
replacing the line above with:
*p = stringPtr;
stringPtr += length + sizeof(UniChar);
p++;
}
return data;
}
HTH,
Brant
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.