GP32 Help Reading From Arrays


Still Fresh
Jun 12, 2004
A ver si algun programador caritativo me puede echar una mano, que me estoy volviendo loco con esto.

I have a function that loads from the smc some complex structures, most of the time accesing byte by byte, so it wastes a lot of time.

To avoid this, I'm trying to load in memory the entire file and then select the important data from there. To do so I've used gpbinconv, a little program that creates an array of bytes from any binary file.

My function is:

unsigned short A;
unsigned int B;

smc_fread (&A, 2, 1, file);
smc_fread (&B, 4, 1, file);

It reads 2 bytes from file and stores them into A, next stores into B the next 4 bytes. OK, this works well but very slow.

Now I have "array", storing all the data from "file". The code will be as follows:

unsigned short A;
unsigned int B;
int pos = 0;

const unsigned char array[9730] = {77, 77, 2, 38, 0, 0, .........};

A = ((unsigned short *)array)[pos];
pos += 2;

B= ((unsigned int *)array)[pos];
pos += 4;

Now this code reads OK the first time (should read 0x4d4d and so it does) but after that it reads '10' (0x0a) when it should be '9730' (0x00002602) ).

Why it only reads 4bits when it should read 32? And odder, the first case works well... :blink:

I think that the error can lay in the order in which I'm reading the bytes, but I'm not able to find it. Something to do with Big or Little Endian?

Ok, thanks for the time spent reading this post (and trying to understand my english :D ) I hope someone can help my, 'cos I'm in a hurry.

Your array is considered as a byte array I guess. When you access it using pos, it should read only the 1st byte, which is not good. Don't know why it works the 1st time though..

But I think using this it should work:
A = ((unsigned short *)(array + (pos >> 1) );
pos += 2;

B= ((unsigned int *)(array + (pos >> 2) );
pos += 4;

Hope this works.
No, didn't work.

But I managed to fix the problem, I totally forgotted the memcpy funtion, which makes all the job on it's own... :unsure: Next time I'm sure I won't forget it xD