Re: Reading bytes from a file and using them
Re: Reading bytes from a file and using them
- Subject: Re: Reading bytes from a file and using them
- From: Sherm Pendley <email@hidden>
- Date: Mon, 2 Jun 2003 21:46:08 -0400
On Monday, June 2, 2003, at 04:05 PM, Toon Van Acker wrote:
I can create NSData from a file, but how do I read certain bytes from
that file?
If you'd like to use NSData, but you don't want to read the whole file
into memory at once, you can use NSData's +dataWithContentsOfMappedFile:
or -initWithContentsOfMappedFile: methods to map the file into your
program's address space. It's then treated as virtual memory, and blocks
of the file are only paged in or out when needed; that is, when the
corresponding memory addresses are read and/or written.
NSData has several methods for accessing bytes - for a raw pointer,
there's -bytes, for copying bytes into a buffer that you allocate,
there's the various -getBytes* methods, and for deserializing specific
types, you can use any of the various -deserialize* methods.
And how do I compare them with hex?
You can use hex values directly in your code, by prefixing them with 0x.
Comparing against a hex number is the same as comparing against an
ordinary base-10 number:
if ( 0xF0 == b ) { ... }
If you have a string that you know contains a hex value, you need to
convert it from a string into an int before you can do a numeric
comparison against it. NSScanner's -scanHexInt: method is useful for
that. Assuming that hexString is an NSString object that holds a string
with a hex value:
unsigned int b;
if ([[NSScanner scannerWithString: hexString] scanHexInt: &b]) {
if ( 0xbeef == b ) {
NSLog(@"Moo!");
}
}
How do I check each bit of a byte?
You can check the value of an individual bit by using a bit mask and the
bitwise AND operator. For example, if you have an int and you want to
check if any of the three lowest-order bits are set, you'd create a bit
mask with just the bits you want to check set to 1, and then do a
logical AND. If the result is non-zero, then one of the bits you looked
at were set.
// 0111 = 7
unsigned int bitMask = 7;
if ( 0 != (b & bitMask) ) {
NSLog(@"One or more of the low three bits are set");
}
To set individual bits to 1, use a mask and the bitwise OR operator. Set
the bits in the mask that you want to enable in the target, OR the mask
with the target, and the result will have those bits enabled, along with
whatever other bits were already enabled in the target.
unsigned int target = 8; // 1000
unsigned int mask = 7; // 0001
NSLog(@"Target OR Mask is %d", target | mask); // 9, i.e. 1001
To clear individual bits, use a mask and the bitwise AND operator. Set
all of the bits in the mask *except* those you want to clear in the
target, and AND the mask with the target. The one's complement operator
is useful for creating the mask for this:
unsigned int target = 9; // 1001
unsigned int mask = 1; // 0001
mask = ~mask; // mask is now 1110
NSLog(@"Target AND Mask is %d", target & mask); // 8, i.e. 1000
sherm--
"I have no special gift, I am only passionately curious." - Albert
Einstein
_______________________________________________
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.