• 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
Re: zeroing out unused bytes from NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >zeroing out unused bytes from NSData (From: Salánki Benjámin <email@hidden>)

  • Prev by Date: Re: PB hangs forever when compiling
  • Next by Date: [OT] Apple Design Awards
  • Previous by thread: zeroing out unused bytes from NSData
  • Next by thread: Re: zeroing out unused bytes from NSData
  • Index(es):
    • Date
    • Thread