UUID和网络上的字节交换

时间:2009-07-23 05:11:33

标签: networking binary uuid

是否有标准的字节交换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 ......?

感谢。

1 个答案:

答案 0 :(得分:3)

是的,这是正确的做法。

每个号码(time_lowtime_midtime_hi_and_version)都按字节顺序排列。 node字段不是。 clock_seq_hi_and_reservedclock_seq_low也受字节排序的影响,但它们都是一个字节,因此没关系。

当然,您需要确保在两端选择正确的顺序,或者如果您不控制它,请了解另一端的顺序。微软当然使用小端为他们的UUID。您可以看到Microsoft对UUID here的定义。

相关问题