PHP 中规范 uuid 表示的 16 字节二进制形式
如何从字符串/规范表示中获取 uuid 的 16 字节二进制形式:
例如:1968ec4a-2a73-11df-9aca-00012e27a270
干杯, /马尔辛
how can I get 16 bytes binary form of the uuid from its string/canonical representation:
ex:1968ec4a-2a73-11df-9aca-00012e27a270
cheers,
/Marcin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包
pack
如果您准确阅读了有关 DCE 定义的 UUID 的格式和字符串表示形式的章节,那么您就不能天真地将 UUID 字符串视为十六进制字符串,请参阅 UUID 的字符串表示形式(从 微软开发者网络)。
即因为前三个字段以大端表示(最高有效数字在前)。
因此,在运行 PHP 32 位的小端系统上最准确的方法(也可能是最快的)是:
它将字符串拆分为字段,将十六进制表示形式转换为十进制数字,然后通过
打包
。因为我无法访问大端架构,所以我无法验证这是否有效,或者是否必须使用例如不同的格式说明符来打包。
If you read accurately the chapter about the format and string representation of a UUID as defined by DCE then you can't naively treat the UUID string as a hex string, see String Representation of UUIDs (which is referenced from the Microsoft Developer Network).
I.e. because the first three fields are represented in big endian (most significant digit first).
So the most accurate way (and probably the fastest) on a little endian system running PHP 32bit is:
It splits the string into fields, turns the hex representation into decimal numbers and then mangles them through
pack
.Because I don't have access to a big endian architecture, I couldn't verify whether this works or one has to use e.g. different format specifiers for
pack
.