C++ UUIDs are stored as integers

C++ UUIDs are stored as integers … here is a solution to the problem.

C++ UUIDs are stored as integers

Is it possible to convert UUIDs to 128-bit integers using C++ under Linux. After converting the UUID to an integer, I need to convert the 16-byte integer to a byte array so that I can send it to another server via TCP.

As far as I know (correct me if I’m wrong), there is no out-of-the-box support for handling 16-byte integers in C++, can you shed light on how to do this? If I can’t store and send it as an integer, I’m thinking about sending the uuid as a string.

Solution

Why is it represented as a 128-bit integer?

How is the UUID represented? Probably it’s a string or already some 16-byte type. If it is a string, then you can calculate the 16-byte array representation yourself, and if it is already some 16-byte long type, then you can directly think of it as a 16-byte array.

As always, whatever you do, you must ensure that all parties agree on the byte order represented by the network.


C++ implementations do not need to have 128-bit integer types, but one of the built-in types may be 128-bit. Long long, maybe. Or your implementation might provide extended integer types that you can use.

Clang and I think GCC both support __uint128_t types, but unfortunately don’t follow C++’s requirements for this type of extended integer. But the 128-bit algorithm should work, so you should be able to do normal to paste the UUID into this type and then treat it as a 16-byte array.

Related Problems and Solutions