How to serialize/Deserialize a String value?

81 views
Skip to first unread message

Sedrati Foued

unread,
Jul 23, 2022, 11:02:05 AM7/23/22
to ns-3-users
hello, 
I want to send a string value in the packet header, I added the variable but I don't know how to serialize and desirialize a string  : for Integer we use the methodes (i.WriteU8(variable) and i.WriteU16(variable) ect.. ).

what are the functions used for a string variable?

Thank you.

Tom Henderson

unread,
Jul 23, 2022, 11:42:17 AM7/23/22
to ns-3-...@googlegroups.com, Sedrati Foued
You probably want this method:

/**
* \param buffer a byte buffer to copy in the internal buffer.
* \param size number of bytes to copy.
*
* Write the data in buffer and advance the iterator position
* by size bytes.
*/
void Write (uint8_t const*buffer, uint32_t size);


If you have

std::string m_string;

as a member of your header class (MyHeader), when you are in Serialize()
method you should be able to do something like:

virtual void Serialize (Buffer::Iterator iter) const {
...
iter.Write (m_string.c_str(), m_string.length ());
...
}

You can do something similar using the matching Read() method. You
will need to pay attention to the size-- you need to read out the same
number of bytes that you wrote in (but you won't necessarily know the
size of the string at Read () time). So, you probably will want to pad
your string out to a well-defined fixed size and use that size value on
the Read () method.

- Tom

MyHeader hdr;
hdr.Write (m_str.c_str (), m_str.length ());

>
> Thank you.
>
> --
> Posting to this group should follow these guidelines
> https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
> <https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting>
> ---
> You received this message because you are subscribed to the Google
> Groups "ns-3-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ns-3-users+...@googlegroups.com
> <mailto:ns-3-users+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ns-3-users/e103cc34-380f-4cc8-b78b-07a1e750ccc9n%40googlegroups.com
> <https://groups.google.com/d/msgid/ns-3-users/e103cc34-380f-4cc8-b78b-07a1e750ccc9n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Sedrati Foued

unread,
Jul 31, 2022, 3:00:49 AM7/31/22
to ns-3-users
thank you for the reply,

I tried what you have mentioned and this error came out 

Screenshot_9.png

Tom Henderson

unread,
Jul 31, 2022, 11:50:02 AM7/31/22
to ns-3-...@googlegroups.com, Sedrati Foued
On 7/31/22 00:00, Sedrati Foued wrote:
> thank you for the reply,
>
> I tried what you have mentioned and this error came out

Sorry, you need an additional cast, as described here:
https://stackoverflow.com/a/7664596

- Tom
> <https://groups.google.com/d/msgid/ns-3-users/e103cc34-380f-4cc8-b78b-07a1e750ccc9n%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ns-3-users/e103cc34-380f-4cc8-b78b-07a1e750ccc9n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> Posting to this group should follow these guidelines
> https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
> <https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting>
> ---
> You received this message because you are subscribed to the Google
> Groups "ns-3-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ns-3-users+...@googlegroups.com
> <mailto:ns-3-users+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ns-3-users/bdad69db-d47c-43d5-815a-19314e15bf57n%40googlegroups.com
> <https://groups.google.com/d/msgid/ns-3-users/bdad69db-d47c-43d5-815a-19314e15bf57n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Sedrati Foued

unread,
Jul 31, 2022, 1:47:42 PM7/31/22
to Tom Henderson, ns-3-...@googlegroups.com
thank you

pdbarnes

unread,
Aug 3, 2022, 12:20:10 AM8/3/22
to ns-3-users
Another common technique for variable-sized objects is to serialize the size, then the data.  In this case

virtual void Serialize (Buffer::Iterator iter) const

  ... 
  iter.WriteHtolsbU64 ((uint64_t)m_string.size())
  iter.Write(m_string.c_str(), m_string.length ()); 
  ... 
}


Reply all
Reply to author
Forward
0 new messages