1

是否有标准可接受的方式来字节交换 UUID 以通过网络(或文件存储)传输?(我想发送/存储 16 个原始字节,而不是将 UUID 转换为字符串表示形式。)

起初我想我应该将 UUID 划分为 4 个 32 位整数并在每个整数上调用 htonl,但这听起来不对。UUID 具有内部结构(来自 RFC 4122):

typedef struct {
    unsigned32  time_low;
    unsigned16  time_mid;
    unsigned16  time_hi_and_version;
    unsigned8   clock_seq_hi_and_reserved;
    unsigned8   clock_seq_low;
    byte        node[6];
} uuid_t;

这样做是否正确:

...
uuid.time_low = htonl( uuid.time_low );
uuid.time_mid = htons( uuid.time_mid );
uuid.time_hi_and_version = htons( uuid.time_high_and_version );
/* other fields are represented as bytes and shouldn't be swapped */
....

写之前,然后对应的ntoh...在另一端读后调用?

谢谢。

4

1 回答 1

3

是的,这是正确的做法。

每个数字 ( time_low, time_mid, time_hi_and_version) 都服从字节顺序。该node领域不是。clock_seq_hi_and_reserved并且clock_seq_low也受字节排序,但它们都是一个字节,所以没关系。

当然,这取决于您确保在两端选择正确的顺序,或者如果您不控制它,则了解另一端的顺序是什么。微软当然使用 little endian 作为他们的 UUID。您可以在此处查看 Microsoft 对 UUID 的定义。

于 2009-07-23T06:18:35.517 回答