 |
 |
|
 |
06-30-2009, 09:36 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 17
|
loading bytes from file
I'm experiencing some weird problem.
I've created a file, 2400 bytes long. Each bit in those bytes represents a (white/black) pixel.
I load the file like this:
Code:
NSData *data = [NSData dataWithContentsOfFile:path];
Byte *byteData = (Byte*)malloc([data length]);
memcpy(byteData, [data bytes], i);
but for some reason, the bytes are scrambled... I can't really identify the pattern of that disruption.
I've created the file in windows using C# and wrote it into a file using File.WriteAllBytes(). I analyzed it using C# too, and it was OK there.
Any ideas why it isn't OK in Objective C/Mac? Is there a difference in the way Mac and Windows read bytes? 
or maybe NSData inserts some of its own bytes into the file's bytes?
thanks
|
|
|
06-30-2009, 11:06 PM
|
#2 (permalink)
|
|
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 1,260
|
Quote:
Originally Posted by urip
Code:
NSData *data = [NSData dataWithContentsOfFile:path];
Byte *byteData = (Byte*)malloc([data length]);
memcpy(byteData, [data bytes], i);
|
What is the variable 'i' set to in the memcpy call? Does stepping through the code in the debugger glean any useful information?
__________________
Code free of compiler warnings and errors, and code that won't crash are not mutually exclusive.
How your application performs in the simulator is irrelevant, unless your target audience is people who will run your app only on the simulator.
|
|
|
06-30-2009, 11:40 PM
|
#3 (permalink)
|
|
Sleep Deprived Developer
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: NH
Age: 23
Posts: 68
|
Quote:
Originally Posted by urip
I'm experiencing some weird problem.
I've created a file, 2400 bytes long. Each bit in those bytes represents a (white/black) pixel.
I load the file like this:
Code:
NSData *data = [NSData dataWithContentsOfFile:path];
Byte *byteData = (Byte*)malloc([data length]);
memcpy(byteData, [data bytes], i);
but for some reason, the bytes are scrambled... I can't really identify the pattern of that disruption.
I've created the file in windows using C# and wrote it into a file using File.WriteAllBytes(). I analyzed it using C# too, and it was OK there.
Any ideas why it isn't OK in Objective C/Mac? Is there a difference in the way Mac and Windows read bytes? 
or maybe NSData inserts some of its own bytes into the file's bytes?
thanks
|
One quick thing to try, it sounds like
Code:
[NSData initWithContentsOfFile:path]
may be better to use, but I haven't tested it.
__________________
My Website: http://codeforyou.com/ Discussing anything iPhone, Programming, or just anything interesting.
Last edited by Encryptic; 06-30-2009 at 11:42 PM.
|
|
|
07-01-2009, 12:18 AM
|
#4 (permalink)
|
|
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 1,260
|
Quote:
Originally Posted by Encryptic
One quick thing to try, it sounds like
Code:
[NSData initWithContentsOfFile:path]
may be better to use, but I haven't tested it.
|
The dataWithContentsOfFile method is a "static" class method, not an instance method, so no init/alloc is needed.
Quote:
Originally Posted by urip
...Is there a difference in the way Mac and Windows read bytes? 
|
It just occurred to me that maybe it's an endian issue you're seeing.
__________________
Code free of compiler warnings and errors, and code that won't crash are not mutually exclusive.
How your application performs in the simulator is irrelevant, unless your target audience is people who will run your app only on the simulator.
|
|
|
07-01-2009, 10:16 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 17
|
Quote:
Originally Posted by Kalimba
Code:
NSData *data = [NSData dataWithContentsOfFile:path];
Byte *byteData = (Byte*)malloc([data length]);
memcpy(byteData, [data bytes], i);
What is the variable 'i' set to in the memcpy call? Does stepping through the code in the debugger glean any useful information?
|
Oh, sorry, the variable 'i' contains the [data length] value, which in this specific case is 2400.
The only thing I could tell from stepping through the code is that the problem is in the data I get from the file or in the way I access it (and not in the way I draw it to the screen).
Quote:
Originally Posted by Kalimba
It just occurred to me that maybe it's an endian issue you're seeing.
|
as far as I know, endian is only the way the system interpret two or more bytes into numbers. So if for example I have two bytes:
00110110 11110000
these bits will stay the same no matter what endian is used, and the only thing changed would be the number represented by those two bytes. So I can't really see how it would change anything for me since I'm reading the file as separate bytes and not as full integers. Am I wrong?
Maybe it's something in the way I access the bits?
Code:
NSInteger byteIndex = requestedBitIndex / 8;
NSInteger bitIndexInByte = requestedBitIndex & 7; //the bit index (0-7) in the byte
NSInteger mask = 128 >> bitIndexInByte; //I read the bits from left to right so I take the binary number 10000000 and roll it bitIndex times to the right
if(byteData[byteIndex] & mask != 0)
return YES;
else
return NO;
This is how I access the bits. is there something wrong here? does the endian change anything in here?
is there another reason why the data is scrambled?
thanks again
Last edited by urip; 07-01-2009 at 11:04 AM.
|
|
|
07-01-2009, 05:41 PM
|
#6 (permalink)
|
|
Sleep Deprived Developer
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: NH
Age: 23
Posts: 68
|
Quote:
Originally Posted by Kalimba
The dataWithContentsOfFile method is a "static" class method, not an instance method, so no init/alloc is needed.
It just occurred to me that maybe it's an endian issue you're seeing.
|
Endianness was my first thought, but that would only apply to byte order, I.E: a 32-bit integer when stored in Big Endian vs Little Endian, if he's talking a purely byte data file, it doesn't have any endianness.
__________________
My Website: http://codeforyou.com/ Discussing anything iPhone, Programming, or just anything interesting.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 465 |
| 37 members and 428 guests |
| al31, alexanderlonsky, AXP, bartender, CanadaDev, CHV, dany88, davidloew, delphipgmr, DorkyMohr, dre, gate491, ggalante, grouchal, HUngable, iseff, issya, japaternoster, jorgmart, KennyChong, kev12345, kvoyt, learnSomething, listingboat, lukeca, macstar, MacSteve85, masc2279, mebarron, RegularKid, siemens, Slecorne, Stephane.tamis, supudo, TunaNugget, wheli, zmarco |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,492
Threads: 35,775
Posts: 156,707
Top Poster: smasher (2,448)
|
| Welcome to our newest member, gate491 |
|