Help Needed!! Marshalling Message

T

Terence

Help Need!!!
I am writing a program for a distributed system course.
I need to marshall all the messages in order to send them thru a socket.
The first 8 bytes of the message are used to store the length of the message,
and then the rest are the actual content of the message.
So I have a big char array, what is the easiest way to store an 8-byte integer
across the first 8 characters in the array?

For Examples:

Integer 1:
00 00 00 00 00 00 00 01 ...

Integer 10:
00 00 00 00 00 00 00 0A ...

Integer 256:
00 00 00 00 00 00 01 00 ...

I don't want to calculate each of the byte
How do I put the integer in this array??


Thanks,

Terence
 
T

Thomas Matthews

Terence said:
Help Need!!!
I am writing a program for a distributed system course.
I need to marshall all the messages in order to send them thru a socket.
The first 8 bytes of the message are used to store the length of the message,
and then the rest are the actual content of the message.
So I have a big char array, what is the easiest way to store an 8-byte integer
across the first 8 characters in the array?

For Examples:

Integer 1:
00 00 00 00 00 00 00 01 ...

Integer 10:
00 00 00 00 00 00 00 0A ...

Integer 256:
00 00 00 00 00 00 01 00 ...

I don't want to calculate each of the byte
How do I put the integer in this array??


Thanks,

Terence

First off, I don't understand what "marshalling" of messages is.
Assuming 8 bits per byte, you need an integer of 64 bits.
Check out "sizeof(long long)" if your compiler supports that type.
Otherwise check your compiler documentation to see if there is
a 64-bit integer.

Try setting a pointer to 64-bit integer to the first location
of the message array. This will only work if the byte-ordering
of the message is the same as the compiler's. If not, you may
have to "load" the integer yourself one byte at a time.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
F

Frank Schmitt

Terence said:
Help Need!!!
I am writing a program for a distributed system course.
I need to marshall all the messages in order to send them thru a socket.
The first 8 bytes of the message are used to store the length of the message,
and then the rest are the actual content of the message.
So I have a big char array, what is the easiest way to store an 8-byte
integer across the first 8 characters in the array?

For Examples:

Integer 1:
00 00 00 00 00 00 00 01 ...

Integer 10:
00 00 00 00 00 00 00 0A ...

Integer 256:
00 00 00 00 00 00 01 00 ...

I don't want to calculate each of the byte
How do I put the integer in this array??

The following program shows one way to do it.
Beware: unsigned long long is not a standard c++ type and not
necessarily 64 bits long.

#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>

typedef unsigned long long int64;

std::string marshall(int64 a) {
std::eek:stringstream os;
os << std::setw(8) << std::setfill('0') << std::hex << a;
return os.str();
}

// converts c to hex and fills the first 8 bytes of buf with it
int main() {
int64 c(256);
std::string c_marshalled = marshall(c);
char buf[80] = "xxxxxxxxhello world\n";
std::copy(c_marshalled.begin(),c_marshalled.end(),buf);
std::cout << "buf: " << buf << "\n";
return 0;
}

HTH & kind regards
frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top