Page 1 of 1

Storing and retrieving an integer as two chars in a string.

PostPosted: Fri Jan 25, 2008 5:46 am
by DilloDude
What I'm trying to do is have an array of coordinates stored in a string. Each value will be stored in two characters of the string, making a total of sixteen bits. The value needs to be signed. What I've got at the moment is this:
Code: Select all
int getPos(char *source, int start)
{
    char path[256];
    int tmp;
 
    strcpy(path, source);
 
    tmp = path[start] << 8;
    return path[start + 1] + tmp;
}

void setPos(char *dest, int start, int value)
{
    char path[256];
    char f, s;
 
    strcpy(path, dest);
    f = (value & 65280) >> 8;
    s = value & 255;
 
    path[start] = f;
    path[start + 1] = s;
 
    strcpy(dest, path);
}
which doesn't work correctly, and I think that's because both chars are signed, and that twists things a bit (pun not intended). Any ideas?

Re: Storing and retrieving an integer as two chars in a string.

PostPosted: Fri Jan 25, 2008 10:33 am
by Fuzzy
Why not an array of int?

int path[256]; // 32 bits

or if you want it shorter...
short int path[256]; // 16 bits each -32,768 to +32,767


Conversely, an unsigned char is written

unsigned char Dillo; // allows a char to hold 256 values

unsigned int Fuzzy; // neat, huh? It counts up to 4,294,967,295!

However, you can use

long int byebye; // but it doesnt get any longer on 32 bit machines.

Re: Storing and retrieving an integer as two chars in a string.

PostPosted: Fri Jan 25, 2008 11:59 am
by DilloDude
An int array would be most prefereble, but it needs to be an actor variable, which means the only form of array is a string. I could do a workaround and use a 2d array and have each actor find it's own spot there, if I can't find another solution.

Re: Storing and retrieving an integer as two chars in a string.

PostPosted: Sun Jan 27, 2008 5:04 am
by DilloDude
I think I've discovered now that a lot of the problem is with strcpy. because you often want to insert zero, that makes a null-terminator, ending the string, so it doesn't read it all in. So instead, I tried using:
Code: Select all
for (i = 0; i < 255; i ++)
{
    path[i] = source[i];
}
but GE complains with some strings when you access it as an array (hence why I used a separate string in the functions in the first place).

Re: Storing and retrieving an integer as two chars in a string.

PostPosted: Sun Jan 27, 2008 7:43 am
by Bee-Ant
So, what's this code used for/
Is it necessary?

Re: Storing and retrieving an integer as two chars in a string.

PostPosted: Sun Jan 27, 2008 11:12 am
by DilloDude
The idea was for an actor to store itself an array of coordinates as a path. Due to these complications, I've changed it to use a 2d array of coordinates, which works. In the place where I initially load it in, I'm using an array of bytes (unsigned chars) with a separate number for length, which should work around the problem. Each actor has an integer, so it knows which array of coordinates to look in. It works fine now when initialised by another actor, but soon it'll need to load from a level file, which should work fine also. Storing externally also has other benefits such as having other information about the path there instead of on the actor, so now I can save using two extra actor variables that probably wouldn't have been used much anyway.