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

Help Needed!! Marshalling Message

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Terence wrote:
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

Jul 19 '05 #2

P: n/a
"Terence" <fi****@hotmail.com> writes:
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::ostringstream 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

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.