473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

typecasting

aki
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....

Apr 29 '07 #1
2 3339

"aki" <ak**************@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
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
Apr 29 '07 #2
Malcolm McLean wrote, On 29/04/07 10:29:
>
"aki" <ak**************@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
>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"
Unless, as is quite likely IMHO from the way the OP described it, it is
coming in as binary not text. The OP has not been explicit, so it is
possibly your interpretation of the requirement is correct and mine wrong.
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;
The casting above is completely pointless.
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.
With scanf it still invokes undefined behaviour if the number is out of
range, so it is still difficult to get error checking with sscanf, so I
think strtoul/strtol would still be better.

If, as I suspect, the data is coming in as binary, then you can do
things like:

#define TYPE_OFFSET 0
#define LENGTH_OFFSET 1
#define AUTH_PROT 2

mess.type = buff[TYPE_OFFSET];
mess.len = buff[LENGTH_OFFSET];
mess.auth = buff[AUTH_PROT] << 8 + buff[AUTH_PROT+1];

Adjust for the endianness of the incoming data (you do not need to worry
about the endianness of the receiving machine doing it this way). You
might want to write either small functions or macros to wrap up the
extraction of 16 bit unsigned integers, 32 bit unsigned integers etc.

char is not guaranteed to be 8 bits (although the chances of the OPs
system not using 8 bits bytes is slim), but I'm assuming here that even
if it is not the octets of the network data are being put in to separate
bytes on the receiving system which is why I used 8 rather than CHAR_BIT.
--
Flash Gordon
Apr 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
7
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.