473,383 Members | 1,861 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,383 software developers and data experts.

Marshalling data to a char array

Hey there I have a data structure with a set size of 40 I want to marshall the data from an object into an array of characters for simplistic transmission of data. I am aware of serialization but only familiar with it in higher languages, I am stuck using c.

My question is this. How do I point to an individual character value of the unmarshalled object in order to marshall it into a character array I can throw down the socket pipe?

char dataToSend[40];
I assume I need a loop but have no method to break up host control item struct into an assignable single character form.
dataToSend = (char*)&hostControlItem; throws an error.
dataToSend = (char[40]*)&hostControlItem; throws an error.
dataToSend = (char*[40])&hostControlItem; throws an error.

In pseudo code I need
for (int i = 0; i <40; i++)
{
dataToSend[i] = (char*)&hostControlItem.asBinary([i]);
}

Is there an inbuilt method to convert an object to a character array? google was useless!
May 22 '10 #1
1 3982
Banfa
9,065 Expert Mod 8TB
The is no built in method. Simplistically you could use memcpy. However there are a few things to be cautious of.
  • Structures often have padding, that is data that appears between the structure members so that the members have the correct alignment in memory. Strictly you do not need to transmit this padding, it is never normally accessible to the program so over a low band width link or in a speed critical application you would not want to send that data.
  • If the structure contains any pointers transmitting them to another process/computer will be pointless because they will be meaningless to the other computer/process. You need to transmit what the point to.
  • If the other computer has different endianess to the transmitting computer then copying the values like that will result in incorrect data transfer. You will have all the right byte values but in the wrong places.

Generally it is never a good idea to translate you structures structure directly onto a communication medium. You need to marshal it and unmarshal itat the other end.

Marshalling is not terribly hard, you just make sure you are dealing with basic types only and marshal those types into the data stream in a specific way that is platform independent. That way any platform can unmarshal the data correctly.

So for your structure I would recomend writing a function that takes a const struct pointer and a char * pointer to the buffer (or unsigned char * preferably since the data is raw bytes) and for each individual member of the structure marshals that data into the buffer provided.
May 22 '10 #2

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

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
11
by: Ger | last post by:
Hi, I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible... I would like to use this char array in a different class..anyone some...
16
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit...
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
1
by: Betty | last post by:
When marshalling a char array in a structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does...
3
by: Lee Crabtree | last post by:
I have a managed DLL that I've used to expose a C++ class. One of the functions in this class reads a line out of a file into a buffer of type "unsigned char*". Since all the data is just...
1
by: Pixie | last post by:
We have some C++ code that we need to convert to VB.net. It calls the DeviceIOControl API and uses a char array as the output buffer. We think we need to use marshalling to do this but don't know...
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....
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.