Re: NSData object
Re: NSData object
- Subject: Re: NSData object
- From: John Nairn <email@hidden>
- Date: Fri, 11 Apr 2003 08:44:23 -0600
>
I have a NSData object like this:
>
>
// code snippet
>
NSData *myData = [NSData dataWithContentsOfFile:thePath];
>
>
Now I want to read 2 bytes from this object (from offset 5 and 6).
>
These 2
>
bytes are in a nybble-ized format like this:
>
>
0000llll (offset 5 from start of myData object)
>
hhhh0000 (offset 6 from start of myData object)
>
l = low nybble, h = high nybble
>
>
So I end up here with 1 byte of data. I want to copy that byte to
>
another
>
NSData object at offset 0, lets call that object myData2.
>
>
Can someone help me with this code please?
>
>
Peter
One of my applications reads binary archived files created on a Pentium
processor (which uses reverse byte order for int, long, double, etc, vs
Mac processors) and I have to go through byte by byte to reorganize the
results. The same method should work for you which basic uses C
pointers to bytes in the NSData object. Here are some code pieces (with
untested additions to get bytes 5 and 6):
NSData *archData;
short byte56;
char *ap;
// read the file
archData=[[NSFileManager defaultManager] contentsAtPath:filename];
if(!archData) return NO;
// parce the bytes
ap=(char *)[archData bytes];
// get bytes offset 5 and 6
ap+=5;
byte56=*ap++;
byte56+=((short)(*ap)<<8);
// now copy byte56 to new NSData obeject
Note that [archData bytes] returns a pointer to the bytes which are
then accessed read-only. In my program the files can be large and I
wanted access to bytes but did not want to waste time or memory copying
them. If you need to change the bytes you can use [archData
bytes:buffer] instead to get a copy to the bytes.
------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.mse.utah.edu/~nairn
_______________________________________________
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.