• 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: Reading Hex from a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading Hex from a file


  • Subject: Re: Reading Hex from a file
  • From: email@hidden
  • Date: Sat, 8 Jan 2005 17:18:02 -0800

On Jan 8, 2005, at 4:49 PM, Jon wrote:
Actually, I have since switched it to use open/read, but it does exactly the same thing. here is my code:

(I am almost positive that HEXstr isn't the problem since the NSLog is screwy too. I might be interpreting them as UTF-8 (I am a newbie to this, please be understanding), because I tell it to read 81 bytes and it only outputs four or five characters. It did the exact same with fopen/fread. So, what newbie mistake am I making this time ;)

What is contained in your header? In general, using NSLog() or fprintf() to output binary data is a lose. Any NULL characters will immediately terminate the output (since a (char *) is considered to be a NULL terminated string) and you will likely run into high-bit weirdness in the guise of unicode string mis-interpretation.


if you want to see what bytes are in the stream, you can do this:

    char *hheader;
    int aaoFile;

    NSData *myData = [NSMutableData dataWithLength: 81];

    aaoFile = open([path fileSystemRepresentation],O_RDONLY,"777");

    read(aaoFile,[myData mutableBytes],81);

    NSLog(@"Header %@", myData);

    ...

Or you could:

    NSData *myData = [NSData dataWithContentsOfMappedFile: path];

    NSLog(@"Header %@", [myData subdataWithRange: NSMakeRange(0,81)]);

    ...

dataWithContentsOfMappedFile: takes advantage of mach's memory mapping features. You should be able to map in a 10MB file without actually consuming 10MB of RAM -- the file will only be brought into memory which you touch a page of that file. read() will cause everything read to be copied into your malloc()'d buffer. Depending on needs, this can vastly reduce memory usage.

Of course, you really should check that the file is at least 81 bytes in length for risk of out of range exceptions.

b.bum





_______________________________________________
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: 
 >Reading Hex from a file (From: Jon <email@hidden>)

  • Prev by Date: Re: NSButtonCell Relating To NSTableView Object
  • Next by Date: Re: Continuous controls and undo
  • Previous by thread: Reading Hex from a file
  • Next by thread: Getting a list of specific filetypes from an NSOpenPanel that only selects directories.
  • Index(es):
    • Date
    • Thread