Re: typecasting




"aki" <akhileshrawat007@xxxxxxxxx> wrote in message news:1177838206.040139.210090@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,

i am receiving a buffer from network...
there are n number of options in buffer type char*
each option contain thr fields of fixed size.
1)type
2)length
3)value

example->

struct authProt
{
uint8_t type;
uint8_t length;
uint16_t authprot;

};

have created a char array of pointers to point to all these n
options.
char *options[n];
then how to decode each field of an option.

all comments are welcome....

option string

"10 15 1001"

to decode

int type;
int len;
int ap;

sscanf(options[n], "%d %d %d", &type, &len, &ap);

Then in your structure

struct authProt auth;

auth.type = (unit8_t) type;
auth.length = (uint8_t) length;
auth.authprot = (uint16_t) ap;

You could use strtol instead of sscanf. Strictly you should use a long for ap if it can go over 32767, and you need error checking in sccanf.
You only need the special fixed types in the actual structure. Elsewhere you can manipulate the information with plain types, and often these will be better. For instance if legal types go from 1-100, and someone feeds 123456789012345678901234567890 to the function, a conversion to integer is much more likely to pick up the error than a conversion to an 8 bit value, handy if you have "good enough" error checking.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


.