Re: Converting NSArray of NSString elements into binary?
Re: Converting NSArray of NSString elements into binary?
- Subject: Re: Converting NSArray of NSString elements into binary?
- From: Scott F Bayes <email@hidden>
- Date: Tue, 2 Oct 2007 11:27:54 +0900
Hi Russ,
At the risk of summary ostracization by the list, I'll suggest that
this might be an appropriate place to use classic C stdio tools. I
use something like the following to read binary from a text file
containing hex byte representations to an unsigned char * byte array
originalPixels (which is a a "frame buffer", which is why the nested
loops):
#include <stdio.h>
.
.
.
/* file name is in NSString *fileName */
unsigned char *originalPixels;
int x, y, result, pix;
FILE *f;
[fileName getCString:fname];
f = fopen(fname, "r");
if (!f) {
printf("coin file %s could not be opened\n", fname);
return;
}
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
result = fscanf(f, "%x", &pix);
if (EOF == result) {
/* your error handling here */
}
originalPixels[y*w + x] = (unsigned char)pix;
}
}
(warning: parts of the above typed in Mail)
The intermediate variable pix is not really needed, but it can make
debugging easier.
Replace the %x in fscanf() with %d for decimal text representation.
The %d or %x skips all leading whitespace including newlines, so
shouldn't matter how many numbers you have on a line.
The Cocoa approach looks like a bit more of a struggle than the above.
ScottB
On Oct 2, 2007, at 11:04 , R.L. Grigg wrote:
On Oct 1, 2007, at 6:05 PM, Nick Zitzmann wrote:
On Oct 1, 2007, at 5:28 PM, R.L. Grigg wrote:
Is there some method to convert the NSArray of NSString elements
into binary byte values?
No.
Or do I have to go through each element and convert each NSString
into a NSNumber so I can put it's byteValue into a NSMutableData
one by one? Is this the wrong approach?
Using NSNumber is a little extreme; just using -intValue ought to
cover things unless you need 64-bit integers.
Hmm, I wish there was a -charValue method for NSString. This still
needs a step to go from the integers that intValue gives me to byte
values for putting into NSData. I guess there's no graceful way to
accomplish this.
Russ
_______________________________________________
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:
40kdn.biglobe.ne.jp
This email sent to email@hidden
_______________________________________________
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