简体   繁体   中英

Boost uuid + boost endian

I need to place uuids at locations in memory that I don't control (a pointer is passed to me (to shm backed by file)), but I have to make sure that what I store at the given pointers will be portable, ie I would like to use boost::endian to settle on little endian for the uuids. Is it possible to combine boost::endian and boost:uuid for that?

The endianness of a UUID does matter (contrary to @Ferruccio). The endianness of a UUID can vary by implementation and platform. As discussed here , UUIDs are governed by RFC 4122 which only recommends that UUIDs be stored in big-endian format.

Within a particular implementation or protocol, then yes, you can just do a byte-comparison. (That is one of the benefits of UUIDs.)

For example:

  • RFC4122 recommends big-endian,
  • boost::uuid uses big-endian.
  • Microsoft APIs (eg CoCreateGUID) use little-endian.
  • libuuid/uuidgen uses the native endianness.
  • Python uuid can do big or little but you must specify

So yes, to be portable you should specify what endianness you are using to store them and byte-swap as needed to be consistent.

You don't need to worry about endianness with UUIDs. They are not treated as numeric values. They are simply opaque blocks of 16 bytes. The only meaningful operation you can perform on UUIDs is comparison for equality.

Update: In some cases endianness may matter. eg if you are converting the UUID to a string representation on multiple platforms and you need all those string representations to match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM