• 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: Using NSData to read bytes from a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSData to read bytes from a file


  • Subject: Re: Using NSData to read bytes from a file
  • From: Nick Zitzmann <email@hidden>
  • Date: Wed, 19 Apr 2006 08:29:26 -0600


On Apr 17, 2006, at 2:56 PM, Phil Faber wrote:

Can some kind person please re-write (not just explain the problem), as required, my current code:

	NSOpenPanel *panel = [NSOpenPanel openPanel];
	NSString *inputFile = [panel filename];
	NSData *fileContents = [NSData dataWithContentsOfFile:inputFile];
	oneByte = [fileContents subdataWithRange:NSMakeRange(8,1)];
	NSLog(@"%i",oneByte);

...which successfully prompts the user to select a file (of ANY type - not just text) but displays (via NSLog) the number "3744544" (which presumably is a pointer address).

...into something that will display (again via NSLog) the ASCII value of character 8 in the file?

Instead of using -subdataWithRange: you probably want the data in a C primitive rather than an NSData object, so try this instead:


uint8_t oneByte;

[fileContents getBytes:&oneByte range:NSMakeRange(8, 1)];

Then oneByte will contain the byte that's eight bytes into the file. You might also want to verify that the data object is long enough; - getBytes:range: will throw an NSRangeException if the location supersedes the data object's length.

Nick Zitzmann
<http://www.chronosnet.com/>


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Using NSData to read bytes from a file (From: Phil Faber <email@hidden>)

  • Prev by Date: Re: [Q] COM and dynamic link library?
  • Next by Date: Re: Localization
  • Previous by thread: Using NSData to read bytes from a file
  • Next by thread: Re: [Q] COM and dynamic link library?
  • Index(es):
    • Date
    • Thread