Help Needed for 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

those who know me have no need of my name

in comp.lang.c i read:
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?
I don't want to calculate each of the byte

too bad, that's what you must do. really. don't worry about it, just do it.
 
N

Nils Petter Vaskinn

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?


That's mighty big. Will there ever be messages lager than 2^32 ? If yes it
sound's like you're doing some really heavy duty data shuffling.

This is based on the assumption that you have the same integer
representation on both ends of the wire:

/* untested and never compiled */
#include <stdint.h>

/* copy the bits of value into an array */ foo(char *array, uint64_t
value) {
memcpy(array,&value,sizeof value);
}
 
D

Derk Gwen

# in comp.lang.c i read:
#
# 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?
#
# I don't want to calculate each of the byte

It's a good idea to store bytes in network order: it's possible you have a
header file that define ntohll and htonll.

encode length
char message[...];
long long networkLength;
assert(sizeof networkLength==8);
networkLength = htonll(message-length-expression);
memcpy(message,&networkLength,sizeof networkLength);
decode length
memcpy(&networkLength,message,sizeof networkLength);
message-length-variable = ntohll(networkLength);
 
K

Keith Thompson

Nils Petter Vaskinn said:
This is based on the assumption that you have the same integer
representation on both ends of the wire:

Which is probably an unsafe assumption, especially for distributed
systems.

(N & 0xFF) gives you the low-order 8 bits of N.

(N >> 8) gives you N with the low-order 8 bits removed; if N is
unsigned, the high-order bits will now be 0.

A loop will give you each byte, one at a time.

Even if N is, say, a 32-bit unsigned integer, you can still fill 8
bytes; the upper bytes will be zero.

This assumes 8-bit bytes, probably a safe assumption in this context.
You might consider adding something like this:

#if CHAR_BIT != 8
#error "CHAR_BIT != 8"
#endif

but it could be considered overkill.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top