python struct.pack equivalent in c++

Xinus picture Xinus · Oct 11, 2009 · Viewed 8.5k times · Source

I want a fixed length string from a number just like struct.pack present in python but in c++. I thought of itoa (i,buffer,2) but problem can be that its length will depend on platform. Is there any way to make it independent of platform ?

Answer

Jeremy Friesner picture Jeremy Friesner · Oct 11, 2009

If you're looking for a complete solution similar to Python's struct package, you might check out Google's Protocol Buffers Library. Using that will take care of a lot of issues (e.g. endian-ness, language-portability, cross-version compatibility) for you.