Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cea

macrumors newbie
Original poster
Oct 26, 2009
22
0
Hi,
I does not have programming background. In the following Obj-C code i create an array of bytes but i must convert bytes into string Hex format to work further. It is not a good idea. So i would like to create an array in C directly without converting to string. How can i do it? Thank you!

Something like this in Obj-C + C:

NSData *data = [NSData dataWithContentsOfFile:file];
const unsigned char *bytes = [data bytes];
for (int i = 0; i < [bytes count]; i++) {
unsigned char array = (int)bytes; // Something like this in C
}
printf("array: %d", array);


PHP:
	NSData *data = [NSData dataWithContentsOfFile:file];
	const unsigned char *bytes = [data bytes];
	NSMutableArray *array = [NSMutableArray array];
	
	for (int i = 0; i < [bytes count]; i++) {
		NSString *bytesAsString = [NSString stringWithFormat:@"%02x", (int)bytes[i]];
		[array addObject:bytesAsString];
	}
	
	NSLog(@"%@", array);
 
It seems to me that the second line gets an array of bytes. I don't think you're expressing where you need the data to start and what you want to do with it clearly enough.

-Lee
 
Yes, the secound line gets an array of bytes, then i loop the array to get array components and put them in a new array. I do not know how to store array components in a new array in C.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.