Re: Accessing 8-bit bytes from a file using Cocoa
Re: Accessing 8-bit bytes from a file using Cocoa
- Subject: Re: Accessing 8-bit bytes from a file using Cocoa
- From: Sherm Pendley <email@hidden>
- Date: Mon, 1 May 2006 15:07:24 -0400
On May 1, 2006, at 8:17 AM, Phil Faber wrote:
One list at a time please. Not everyone here subscribes to the Yahoo
list.
I want to be able to read from any file type (.txt, .doc, .xls,
etc) one 'literal byte' (that is, a string of eight ones-and-zeros)
at a time. The file could be any size. For each byte I want to be
able to get the actual ASCII value of that byte (a number from zero
to 255).
ASCII code points range from 0 to 127.
At the moment, I'm using C coding:
(fread(&oneByte,1,1,fp1) [where oneByte is defined as a 'char']
A char is not guaranteed to be one byte. The C standard specifies it
as "at least 8 bits" in size - i.e., it's allowed to be more. If you
want an 8-bit variable, no more nor less, use the size-specific type
"uint8" instead.
(a) this only works for basic ASCII characters (for example, "A"
appears as ASCII 65) but not non-standard characters (for example,
"å" appears as ASCII -116 ... yes, MINUS 116)
You asked for a char, and that's what you got - integral types are
signed by default, i.e. with high-order bit used to indicate
signedness. In the case of an 8-bit char, that gives a range of -127
to 127. If you want an unsigned char, with a range of 0 to 255,
you'll need to declare it as such.
(b) I suspect that it is better to achieve this task with Cocoa
instead of C.
I often use NSData's +dataWithContentsOfMappedFile: to memory-map a
file. Then I can simply call the returned instance's -bytes method to
get a pointer to the data, and let the VM subsystem handle reading &
writing pages as needed.
Can anyone please provide me with the most basic coding to achieve
this in Cocoa?
Have a look at:
http://developer.apple.com/documentation/Cocoa/Conceptual/BinaryData/
index.html
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
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