473,473 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

bytes manipulation in C++

5 New Member
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
2 4676
JosAH
11,448 Recognized Expert MVP
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
12
by: jmoy | last post by:
I have some data (say in a file) that needs to be handled byte by byte. Source code I have looked at does this by treating the data as a stream of 'char's. However, the standard does not require a...
9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
25
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
9
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.?...
8
by: LEM | last post by:
Hi all, Perhaps an easy question, but I haven't been able to find the solution. Let's suppose that I have this: byte a; a=0x01; a=0x6B;
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
6
by: Bint | last post by:
I have an array whose elements I'm accessing, like array, array, etc. However, the data is meant to be 16-bit words, not bytes. I'm getting byte values right now. Is there any way I can tell php...
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...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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: 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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.