Re: typecasting...
On Apr 29, 10:28 am, aki <akhileshrawat...@gmail.com> wrote:
i am receiving a buffer from network as char pointer.
char * buffer;
this is an argument of my function.
well i want to decode this buffer.
buffer contain a packet with different fields
as
code of size 1 byte contain inter value
In what format?
identifier of size 1 byte contain integer value
In what format?
and
length of size 2 byte. contain integer value.
In what format?
as struct lcpHeader
{
uint8_t code;
uint8_t identifier;
uint16_t length;
};
You'll have to extract the individual bytes and reconstruct them
into whatever struct you want to use. There's no guarantee that
the internal format of a struct is in any way related to some
arbitrary network protocol.
i want to access it all fields.
1) code field
struct lcpHeader *hdr;
hdr=(struct lcpHeader*)buffer;
when i do switch(hdr->code)
will it give integer value contained in the code....?
Exceptionally, this one probably will, as long as the possible
values in the code field are in the range [0,128). The one
guarantee you have about structure layout is that the address of
the first element is the same as the address of the struct.
If the code can be negative, or have values greater that or
equal to 128, you'll have to convert the char to either signed
or unsigned char.
how to access other fielsd?
By extracting the bytes from the buffer, recombining them to
form whatever type you want, and assigning the results to the
field in the struct. Without knowing more about the format,
it's impossible to say precisely how to do this.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34