Re: zeroing out unused bytes from NSData
Re: zeroing out unused bytes from NSData
- Subject: Re: zeroing out unused bytes from NSData
- From: Fabian Lidman <email@hidden>
- Date: Wed, 16 Apr 2003 19:56:54 +0200
I have an NSData structure that has unused bytes at the end. How can I
zero out the unused bytes at the end so that my NSDatat will have only
used bytes in it?
Use an NSMutableData object. Assuming that your "unused" bytes are
simply null bytes, it would look something like this (not tested so
don't take it literally):
/* begin example */
NSMutableData mutableData = [NSMutableData dataWithCapacity: 0];
[mutableData appendData: myData];
char* bytes = (char*)[mutableData mutableBytes];
int startOfUnusedBytes;
for( int byteIndex = 0; byteIndex < [mutableData length]; byteIndex++ )
if(bytes[byteIndex] == 0)
startOfUnusedBytes = byteIndex;
[myData autorelease];
myData = [[NSData dataWithBytes: bytes length: startOfUnusedBytes]
retain];
/* end example */
_______________________________________________
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.