Re: Marshalling Message??

From: Stefan Höhne (do_not_use_this_adress_at_127.0.0.1)
Date: 11/11/03


Date: Tue, 11 Nov 2003 15:58:32 +0100

Hi,

"Terence" <findty@hotmail.com> wrote in message
news:0rYrb.17623$HoK.16118@news01.bloor.is.net.cable.rogers.com...
> Help Need!!!

who's 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,

8 Byte? Your're prepared for the future ...

> 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?

Depends on how you represent an 8-byte-integer in memory, thus: depends on
what
is the input.

> 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

But: this would be the cleanest and most portable solution.

... and not that hard (if theres a shift operation on
    your input) ...

#include <algorithm>
#include <iterator>
#include <iostream>
#include <iomanip>

int main() {
  long long int length=266;
  char message[10] = {0};
  int i=9;

  while ((length>0) && (i>=0)) {
    message[i]=length;
    length>>=8;
    --i;
  }
  std::cout << std::hex;
  std::copy(message, message+10, std::ostream_iterator<int>(std::cout, "
"));
}

Output:
0 0 0 0 0 0 0 0 1 a

> How do I put the integer in this array??

OK?

As others suggested, using standard posix functions
would be ok, too: but I doubt they'll be defined for
a 64bit integer.

HTH,
Stefan.



Relevant Pages

  • Re: Showing problem
    ... >> How can i changhe this code showing how many persons i saved to the ... > better to use char array for phonenumber field too.... ... > then it will overflow and store wrong information. ... language standard) a range of values for type 'int' from ...
    (comp.lang.c)
  • Re: Help Needed for Marshalling Message
    ... > marshall all the messages in order to send them thru a socket. ... > char array, what is the easiest way to store an 8-byte integer across ... "the large print giveth, and the small print taketh away" ...
    (comp.lang.c)
  • Re: Help Needed for Marshalling Message
    ... >I need to marshall all the messages in order to send them thru a socket. ... >So I have a big char array, what is the easiest way to store an 8-byte integer ...
    (comp.lang.c)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)