Problem with Serialize/Deserialize of Double

258 views
Skip to first unread message

Konstantinos

unread,
May 12, 2011, 9:55:05 AM5/12/11
to ns-3-...@googlegroups.com
Hey all,

I want to include a "double" value in a hello message that I send. Basically it is the velocity that I include. Since velocity is a 3D Vector, I have to serialize 3 double values.

This is how it I do it...

  i.WriteHtonU64  ((uint64_t)this->velocity.x);
  i.WriteHtonU64  ((uint64_t)this->velocity.y);
  i.WriteHtonU64  ((uint64_t)this->velocity.z);

After a lot of tries and debug messages, I realized that my problem was the fact that velocity can get also negative values but I serialize it as UINT64_T and thus I get values like this 1.84467e+19 which is the max value for a UINT64_T variable. Is there another way to implement this ??

From the ns3::Buffer::Iterator Class Reference
I couldn't find something. Perhaps I should serialize only the magnitude of the velocity and include the heading as a second value as well.

Regards,
Konstantinos

Mohammad

unread,
Sep 20, 2012, 6:22:31 AM9/20/12
to ns-3-...@googlegroups.com
Hello Konstantinos,

I have the same problem to serialize a negative value!!

Have you solve the problem? if so, please write ti down here so other also can use it,

Thanks

Konstantinos

unread,
Sep 20, 2012, 6:56:14 AM9/20/12
to ns-3-...@googlegroups.com
I just serialize the abstract value (always positive) and have another variable to serialize the sign (if you need the value) or the heading (if you just need the velocity)

Jordan Mihaylov

unread,
Sep 20, 2012, 5:14:21 PM9/20/12
to ns-3-...@googlegroups.com
Hi,

You have the Buffer::Iterator i ...
so try the following:

// for Serialization
i.Write((uint8_t*) &velocity, sizeof(velocity));

// for Deserialization
i.Read((uint8_t*) &velocity, sizeof(velocity));

I'm not sure if std::vector has some storage overhead, e.g. if it's size > 3x sizeof(double), so let's suppose that "velocity" in my example represents a simple struct like:

struct vel3d{
    double x;
    double y;
    double z;
} velocity;
Reply all
Reply to author
Forward
0 new messages