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

You must understand the Game Editor concepts, before post here.

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

Postby DilloDude » Fri Jan 25, 2008 5:46 am

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?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

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

Postby Fuzzy » Fri Jan 25, 2008 10:33 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

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

Postby DilloDude » Fri Jan 25, 2008 11:59 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

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

Postby DilloDude » Sun Jan 27, 2008 5:04 am

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).
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

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

Postby Bee-Ant » Sun Jan 27, 2008 7:43 am

So, what's this code used for/
Is it necessary?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

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

Postby DilloDude » Sun Jan 27, 2008 11:12 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest