PDA

View Full Version : Need example...



Amadeus
02-15-2003, 04:46 PM
In Whinge's post, he created a ucpData structure like so:



unsigned char cData[] =
{ SNIFF_PACKET_H1,SNIFF_PACKET_H2,
SNIFF_PACKET_XYZxyzHh,14,
(unsigned char)(iX >> 8),(unsigned char)(iX),
(unsigned char)(iY >> 8),(unsigned char)(iY),
(unsigned char)(iZ >> 8),(unsigned char)(iZ),
0,0,0,0,0,0,
(unsigned char)(iH),0
};


How would one add another chunk of data to this that is of type char[64] (for example) ..or really, any text string as opposed to what is basically integers in this example.

And, how would this affect the size of the struct? Right now it is 14 ..would it be 15 if you added a string with 30 characters, or would it then be 44?

I'm confused on this :) I've figured out how to send various types of data to SEQ using this example; however, I can't get anything but numbers to go through :/ .....big time newbie, I know.

Mr. Suspicious
02-15-2003, 08:05 PM
Each char is a number. A string (char[]) is actually an array of integers with the size of one byte each. With this in mind, I'm pretty sure you'll be able to work things out.

Amadeus
02-15-2003, 10:46 PM
ok...so this is valid? ..and WILL be the same length every time?



unsigned char cData[] =
{ SNIFF_PACKET_H1,SNIFF_PACKET_H2,
SNIFF_PACKET_XYZxyzHh,78,
(unsigned char)(iX >> 8),(unsigned char)(iX),
(unsigned char)(iY >> 8),(unsigned char)(iY),
(unsigned char)(iZ >> 8),(unsigned char)(iZ),
0,0,0,0,0,0,
(unsigned char)(iH),0, (char)pChar->Name[64]
};


...sorry for lame questions, I'm self taught and have used too many custom string classes in my life ;)

huaso
02-16-2003, 12:45 AM
Your code will take the 65th element in the Name array and stick it on the end of the cData array (not a struct). It will increase the size of the array by 1. You might get a compiler warning since you're casting to char and not unsigned char.

Amadeus
02-16-2003, 01:48 AM
ok...so, then am I to do something like this? ..or is there an easier way?

(or would this compile..havn't tried it.) ..perhaps someone can see what I'm failing miserably at trying to accomplish..hehe




//somewhere before we defined 'char buf[2]' as the word "ale"

unsigned char cData[] =
{ SNIFF_PACKET_H1,SNIFF_PACKET_H2,
SNIFF_PACKET_XYZxyzHh,17,
(unsigned char)(iX >> 8),(unsigned char)(iX),
(unsigned char)(iY >> 8),(unsigned char)(iY),
(unsigned char)(iZ >> 8),(unsigned char)(iZ),
0,0,0,0,0,0,
(unsigned char)(iH),0,buf[0], buf[1], buf[2]
};

huaso
02-17-2003, 03:57 AM
That would be one way to do it. Keep in mind that you won't have a null terminator in your example. So if you try to pass your string to a C run-time function like strcpy by using something like &cData[18] then you're going to run into trouble. There are a few ways to accomplish what I think you are trying to accomplish. Another way would be to use the heap and allocate memory for the array plus whatever you needed to append your string to up front, including room for a null terminator. Then you could just strcpy your string to the end of that array. That way you only copy what you need.

Compilers are pretty good these days about telling you what's wrong. Do you not have one available to you? In any event, this thread seems to be more general programming in nature, so this will be my last post on here. Feel free to PM me if you like.