473,466 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

char to int conversion

aki
i am receiving a packet from network and it contains many fields ,
starting from code, idetifier
...., i want to read the code field which is of size 1 byte.
checking the code value i need to go ahed of my logic...

i am receving packet in buffer variable like.

function(*buffer)
{
char*p =(ntohs)buffer;// changin bufferfrom network byte order to
host order
switch(*p) // will it decode to some integer value...if not then how
can i do it...

case 1:
--
break;
case 2:
---
--
}

can i do type conversion..?

any comments are welcome...

thanks
akhi

Apr 23 '07 #1
4 2357
aki wrote:
i am receiving a packet from network and it contains many fields ,
starting from code, idetifier
...., i want to read the code field which is of size 1 byte.
checking the code value i need to go ahed of my logic...

i am receving packet in buffer variable like.

function(*buffer)
{
char*p =(ntohs)buffer;// changin bufferfrom network byte order to
host order
That's garbage, post the real code.

--
Ian Collins.
Apr 23 '07 #2
On 23 Apr, 11:36, aki <akhileshrawat...@gmail.comwrote:
i am receiving a packet from network and it contains many fields ,
starting from code, idetifier
..., i want to read the code field which is of size 1 byte.
checking the code value i need to go ahed of my logic...

i am receving packet in buffer variable like.

function(*buffer)
function(char* buffer)
{
char*p =(ntohs)buffer;// changin bufferfrom network byte order to
host order
char* p = ntohs(*buffer)

It's a function-call, but it operates on 16-bit shorts so you have to
dereference the pointer in the call and make sure that the buffer is
at least 16 bits large. Notice that if you only operate on chard/bytes
then you don't need ntohs() since there's only one way to represent a
byte on both the network and on the machine.
switch(*p) // will it decode to some integer value...if not then how
can i do it...

case 1:
--
break;
case 2:
---
--

}
Have you tried the above? It seem to be OK to me but I'm not totally
sure. Since p points to a char and a char can be promoted to an int I
think the above should work, but if you want to be sure you can always
cast the char to an int first and perform the switch over the int.

--
Erik Wikström

Apr 23 '07 #3
On Apr 23, 12:02 pm, Erik Wikström <eri...@student.chalmers.sewrote:
On 23 Apr, 11:36, aki <akhileshrawat...@gmail.comwrote:
i am receiving a packet from network and it contains many fields ,
starting from code, idetifier
..., i want to read the code field which is of size 1 byte.
checking the code value i need to go ahed of my logic...
i am receving packet in buffer variable like.
function(*buffer)
function(char* buffer)
{
char*p =(ntohs)buffer;// changin bufferfrom network byte order to
host order
char* p = ntohs(*buffer)
It's a function-call, but it operates on 16-bit shorts so you have to
dereference the pointer in the call and make sure that the buffer is
at least 16 bits large.
Also, in practice, I can't imagine it being much used today. It
represents an early attempt to manage different representations
of the same data type, but it doesn't really solve the problem,
and we know of better solutions today.
Notice that if you only operate on chard/bytes
then you don't need ntohs() since there's only one way to represent a
byte on both the network and on the machine.
Sure there are---at least one system sold today has nine bit
one's complement bytes, for example. And the problem isn't just
representing a byte. No one really cares about "bytes"; what's
interesting is what the byte is representing, and how it is
encoded.

Network protocols transfer "octets" (most, at least---although
in the past, I worked with one that used sextets---six bit
bytes). Presumably, from what he is saying, his code field is
encoded on a single octet. The question is, of course, how is
it encoded? Without knowing that, there's not much we can say.
If it's encoded as a small integral value, then the normal
integral promotions in C++ will suffice to access it, at least
if the value is guaranteed positive (less than 128, and greater
than or equal to 0). If it's encoded as an ASCII character,
then he can probably access it directly as well, as long as his
code doesn't have to run on a mainframe. (IBM mainframes still
use EBCDIC, and I have no idea what Unisys mainframes use, with
their 9 bit bytes.) Even on a mainframe, he can still access
the data as a character as long as he explicitly compares it to
the ASCII encoding, rather than a character constant (i.e. 0x30,
rather than '0'---which is 0xF0 on an IBM mainframe).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 24 '07 #4
James Kanze wrote:
On Apr 23, 12:02 pm, Erik Wikström <eri...@student.chalmers.sewrote:
>>It's a function-call, but it operates on 16-bit shorts so you have to
dereference the pointer in the call and make sure that the buffer is
at least 16 bits large.


Also, in practice, I can't imagine it being much used today. It
represents an early attempt to manage different representations
of the same data type, but it doesn't really solve the problem,
and we know of better solutions today.
From what I have seen, this family of ordering functions are still
widely used. They have been updated (at least in POSIX) to use the C99
fixed width types. So ntohs is now

uint16_t ntohs(uint16_t);

--
Ian Collins.
Apr 24 '07 #5

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

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
10
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did...
4
by: jim_geissman | last post by:
I have data tables that include ZIP code, as char(5). The values look like integers, but they are padded with leading zeroes to fill out 5 characters, '00234'. There are SPs to look up data,...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
8
by: AGRAJA | last post by:
how to convert unsigned to char? Ref: http://www.thescripts.com/forum/thread477545.html how do I print without the leading ffffff (yet the result should be char*)?
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.