By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

bytes manipulation in C++

P: 5
Hello all.

I am mostly a C# developer, and I am working on a research project which involves networking between a server written in C# and the client written in C++.

I have defined a protocol that will move info to/from the server, C# socket functions gets bytes from the network, while C++ char*.

So I am running in problem to make C++ to handle and convert values to bytes, I know there is no naive type of byte in C++, but I think char will do that, because a char is a one byte size type in C++. (Visual C++).

the first segment of the protocol is a 2 byte value, that tell the length of the message, in both C++ and C# this value is an unsigned short, which is 2 bytes.

while converting unsigned short to char[2], I run into trouble when the value of the short is less than 255, becuase that way it fits in one byte, then what is received on the C# side is not correctly interpreted.

this is the code I am using to convert from unsigned short to char[2]


unsigned short m_length = 6;
char *data = new char[m_length];
memset(data,'\0',m_length); // All the data block to be sent.

char clen[2];
*(unsigned short *)clen = m_length;

// clen is clen[0] = 6, clen[1] = 0, becuase it is intel and stores the value in
// little ending.

strcat(data,clen);
// I spect that data[0] will have 6 and data[1] 0.
strcat(data,"O");
// and after this I need data[2] = 'O'.

Becuase clen[1] is 0 byte it is ignore when I call the strcat, I am not sure if the string concatenation function is the right to use in here, but the thing is that somehow I need to conver all to char * to be able to send it over the socket.

Some one has a more better approach to solve this?
Aug 5 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
Hello all.

I am mostly a C# developer, and I am working on a research project which involves networking between a server written in C# and the client written in C++.

I have defined a protocol that will move info to/from the server, C# socket functions gets bytes from the network, while C++ char*.

So I am running in problem to make C++ to handle and convert values to bytes, I know there is no naive type of byte in C++, but I think char will do that, because a char is a one byte size type in C++. (Visual C++).

the first segment of the protocol is a 2 byte value, that tell the length of the message, in both C++ and C# this value is an unsigned short, which is 2 bytes.

while converting unsigned short to char[2], I run into trouble when the value of the short is less than 255, becuase that way it fits in one byte, then what is received on the C# side is not correctly interpreted.

this is the code I am using to convert from unsigned short to char[2]


unsigned short m_length = 6;
char *data = new char[m_length];
memset(data,'\0',m_length); // All the data block to be sent.

char clen[2];
*(unsigned short *)clen = m_length;

// clen is clen[0] = 6, clen[1] = 0, becuase it is intel and stores the value in
// little ending.

strcat(data,clen);
// I spect that data[0] will have 6 and data[1] 0.
strcat(data,"O");
// and after this I need data[2] = 'O'.

Becuase clen[1] is 0 byte it is ignore when I call the strcat, I am not sure if the string concatenation function is the right to use in here, but the thing is that somehow I need to conver all to char * to be able to send it over the socket.

Some one has a more better approach to solve this?
Basically you're manipulating binary data there so I shouldn't use any string
functions on it if I were you; better use memcpy() and its compadres.

kind regards,

Jos
Aug 5 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
unsigned short m_length = 6;
...
char clen[2];
*(unsigned short *)clen = m_length;
does not convert a short to a char. clen is a char* and calling it a short* does not make it a short*.

This forces the memcpy(), which is a big no-no- in C++ since constructor/destructor/assignment rules are not applied to what is copied which a) breaks encapsulation, b) opens the door to program crashes.

The problem here is that the automatic conversion rules provide you can assign any integer to any other integer but the result is the responsibility of the programmer. That is, the target type must be able to hold the value.

I recommend the result be an array of 2 unsigned char. Rather than create a new variable, just map the result on top of the data:
Expand|Select|Wrap|Line Numbers
  1.  
  2. unsigned short data = 754;
  3. unsigned char* result = (unsigned char*) data;
  4.  
After the cast result[0] and result[1] is your short as an array of unsigned char. Now you can use these bytes in whatever order the endian requires.
Aug 5 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.