Re: Trouble with binary data and byte buffers
Re: Trouble with binary data and byte buffers
- Subject: Re: Trouble with binary data and byte buffers
- From: Andrew Farmer <email@hidden>
- Date: Mon, 13 Jul 2009 22:14:09 -0700
On 13 Jul 2009, at 22:02, Chase Meadors wrote:
Hi, I'm having some confusion here over displaying raw binary data.
I have my NSMutableData object that I'm getting the bytes from. As I
understand it, there are different ways to get the bytes.
The -getBytes:range: method
NSRange r = NSMakeRange(x, y);
char buf[y];
[data getBytes:&buf length:r];
getBytes:length: takes a scalar argument for length, not a NSRange
object. You're probably thinking of getBytes:range:. However, I'd
recommend strongly against using stack allocation (particularly
variable-length stack allocation) for a buffer of unknown, potentially
large size.
Or the -bytes or -mutableBytes methods
char buf = [data bytes];
What is the generally accepted method of doing this? I'm using the
first method, as the other one sometimes crashes.
It won't crash if you assign it to a variable of the proper type:
char *buf = [data bytes];
Second, I'm testing the data with the following code:
for (int i = 0; i < aNumber; i ++) {
NSLog(@"X", buf[i]);
}
This is what's really bothering me because I'll get normal bytes,
like 2E 00 AA, but sometimes, seemingly randomly, there are six
leading F's.
Hardly random. It's occurring for every value with the high bit set
(0x80 - 0xff).
Read: http://en.wikipedia.org/wiki/Sign_extension
Then switch "char" to "unsigned char" and you'll get the results
you're expecting.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden