473,405 Members | 2,187 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,405 software developers and data experts.

reinterpret_cast'ing a byte array

2
I figured the subject would get everyone's attention. So when dealing with sockets, you receive an array of unsigned char's, also known as bytes. One way to interpret data from those bytes is to make a class with all your needed fields (or struct, it doesn't matter), and do something like this:

Expand|Select|Wrap|Line Numbers
  1. MyClass *translatedData = reinterpret_cast<MyClass*>(&rawBytesReceivedFromSocket);
Obviously this is not ideal. I have to check that the payload is sane before doing something like this. The bytes' structure has to line up with my class exactly. Also, something just doesn't sit well with me, when essentially I'm doing a c-style translation in c++ clothing (if that makes any sense). Not very secure either, treating raw-bytes this way.There has to be a better, OO way. Any ideas?

Joe
Oct 21 '08 #1
1 5484
Banfa
9,065 Expert Mod 8TB
Well the way I choose is this, assuming like me you may be receiving several different messages/formats then
  • You have a 2 level class hierarchy, the base class is abstract and contains a load of protected helper methods as well as a few public interface methods.
  • Some of the protected methods are methods to extract and insert 1, 2 or 4 byte integers, strings and floats from the buffer
  • The interface methods include methods to verify the message is formatted correctly as well as a single method to handle the message.
  • There is 1 subclass for each message you might receive.
  • There is a static public factor method on the base class that given a buffer will create a class of the correct type to hold it. It returns a pointer to the base class.
  • Each subclass has specific knowledge of the things that particular message carries and provides methods that call the helper functions to extract the data.
  • You implement something like the visitor pattern to allow handling of each message.

So when you receive a buffer you call the factory method to create a class of the correct type and this returns a base class pointer to it. The class stores the raw data of the buffer you then verify the message format and discard it if invalid.

You pass the message (the pointer) to the message handling process. This process calls the message handling method on the base class pointer which results in a callback with a sub-class pointer. You can now access all the data in the message and deal with it as appropriate.


Instead of pointers it would be a good idea to use a handle template class (handle design pattern). As I was using C++/CLR (.NET in C++) where handles are a built in part of the language and are the reference of choice I did not need to do this.
Oct 21 '08 #2

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

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
9
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte...
15
by: Aman | last post by:
Hi, wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3 trying to see the byte order of an int or short int by converting to char* . doesn't work . the char* cpt doesn't seem to be...
13
by: Jakob Bieling | last post by:
Hi, I am trying to determine the endianness of the system as follows: int i = 1; bool is_little = *reinterpret_cast <char*> (&i) != 0; But now I was asking myself, if this use of...
1
by: Carl | last post by:
Hi, I have an array of bytes like Byte m_Data = new Byte ; Byte sequences starting at a random index (4 byte aligned) should be interpreted as unsigned long values and compared with each...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
4
by: tkirankumar | last post by:
Hi all, This is regarding the issue I am facing while porting the my application from SuSe Linux to Sun Solaris. The Existing system details: Itanium boxes 1mhz 4 processor 8 gb machines with...
5
by: zr | last post by:
Hi, Is there a way to initialize a std::tr1::array with a pre-allocated built-in array in a copy-less assignment, such that both will point to the same memory? Vice-versa is easy to do, simply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.