How to achieve cross-platform serialization, working directly with bits, composing packages for sending between classes, provided that bit manipulations must be correct for little endian and big endian .

  • 3
    It is interesting what “sending packets between classes” means, classes as if in memory operate on structures, and packets go on the network. for good, you just need to make a permutation function of bytes for each base type larger than 1 byte. on one platform, it returns the number itself, on the other it swaps the bytes - Mike
  • Bit operations (including >> and << ) and so cross-platform. - Abyx
  • I think the term "packages between classes" is associated with communication between process threads, as well as communication between processes. Perhaps the wording about the packages is not unambiguous, for which I apologize, but the connection to the network is not affected. But the problem is clear: the code of bit operations on different architectures - rikimaru2013
  • 2
    @ rikimaru2013 when communicating within one system, there is no point in considering the order of bytes. all threads, processes, etc. work with one order. the question actually arises only when transferring information to another platform - Mike
  • @Mike Thank you did not know. Problems will turn out when you need to make friends with another application (over the network or else how). - rikimaru2013

3 answers 3

When communicating within the same system, taking into account the order of bytes makes no sense. all threads, processes, etc. work with one order. The question arises only when transferring information to another platform. Those. data should be reduced to a predetermined byte order only when transmitting over a network or when writing to files that can later be transferred to another system.

    In the fields of the structures used for the exchange, store the data in a network format (network order, you can find out more here ).

    You can use the htons()/htonl()/ntohs()/ntohl() from the Berkeley sockets API to convert data between the network format and the host format.

    • If big endian is called "network format" it will not become more popular. Most systems use little endian (x86), so why waste time converting to BE and back if both the client and the server are most likely LE? - Abyx
    • @Abyx, probably out of habit. Once (back in the times when RPC and NFS were only sculpted) people implicitly (or, obviously, I don’t remember) already agreed on this (by the way, then big endian (VAX, pdp) was more common among developers) - avp
    • one
      Still confused. VAX and PDP were little-endian, and MC68xxx big-endian. In any case , it will take a lot of time for the Internet Protocol Standard (STD 2) to be big-endian - avp
    • @Abyx, by the way, Java big-endian (and this is a bunch of progs on android), so the question of the prevalence of the format clearly remains open. - avp

    Inside a byte, bits always go from left to right from the highest to the lowest, regardless of the received byte order in the system. The same applies to operands for operators >> << , even if they are numbers consisting of more than one byte. That is, int i=4; i>>=1; int i=4; i>>=1; i will always be 2.

    For cross-platform (de) serialization, you can use htons () / htonl () / ntohs () / ntohl ()