• 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
NSData and Primitives
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSData and Primitives


  • Subject: NSData and Primitives
  • From: Buzz Andersen <email@hidden>
  • Date: Sun, 09 Jun 2002 17:46:40 -0600

Hello all,
I am a long-time Java programmer who is currently trying to very hard to
"grok" the whole Objective C/Cocoa way of doing things, so please bear with
me if this question seems too elementary :-)...

Basically, what I have is a file containing a custom binary data format,
from which I would like to read various primitive data types--longs, Unicode
Strings, etc. Now, I have already done Java programs which do this very
thing using methods like "readLong()" in the java.io.DataInputStream class,
but I am unsure of the best way to accomplish a similar thing using NSData,
NSFileHandle, etc.

Basically, my attempts so far go something like this:

NSFileHandle *file;
NSData *buffer;
unsigned char[4] bytes;
unsigned long number;

file = [NSFileHandle fileHandleForReadingAtPath: theFile];
buffer = [file readDataOfLength: sizeof(long)];
[buffer getBytes: bytes];
memcpy(&number, &bytes, sizeof(long));

Is this the best way to do it, or is there a better way using Foundation or
Appkit classes? Is this even a reliable way of doing it (I'm kind of having
trouble figuring out if this method is working properly--the results don't
seem right to me).

Any help would be GREATLY appreciated :-)!

Thanks,
Buzz Andersen
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSData and Primitives
      • From: Esteban <email@hidden>
    • Re: NSData and Primitives
      • From: Aram Greenman <email@hidden>
  • Prev by Date: Re: Objective-C Specification
  • Next by Date: Re: Objective-C Specification
  • Previous by thread: Process Launch Notification
  • Next by thread: Re: NSData and Primitives
  • Index(es):
    • Date
    • Thread