PHP 中规范 uuid 表示的 16 字节二进制形式

发布于 2024-08-26 03:19:09 字数 118 浏览 6 评论 0原文

如何从字符串/规范表示中获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2024-09-02 03:19:09
 $bin = pack("h*", str_replace('-', '', $guid));

 $bin = pack("h*", str_replace('-', '', $guid));

pack

君勿笑 2024-09-02 03:19:09

如果您准确阅读了有关 DCE 定义的 UUID 的格式和字符串表示形式的章节,那么您就不能天真地将 UUID 字符串视为十六进制字符串,请参阅 UUID 的字符串表示形式(从 微软开发者网络)。
即因为前三个字段以大端表示(最高有效数字在前)。

因此,在运行 PHP 32 位的小端系统上最准确的方法(也可能是最快的)是:

$bin = call_user_func_array('pack',
                            array_merge(array('VvvCCC6'),
                                        array_map('hexdec',
                                                  array(substr($uuid, 0, 8),
                                                        substr($uuid, 9, 4), substr($uuid, 14, 4),
                                                        substr($uuid, 19, 2), substr($uuid, 21, 2))),
                                        array_map('hexdec',
                                                  str_split(substr($uuid, 24, 12), 2))));

它将字符串拆分为字段,将十六进制表示形式转换为十进制数字,然后通过 打包

因为我无法访问大端架构,所以我无法验证这是否有效,或者是否必须使用例如不同的格式说明符来打包。

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:

$bin = call_user_func_array('pack',
                            array_merge(array('VvvCCC6'),
                                        array_map('hexdec',
                                                  array(substr($uuid, 0, 8),
                                                        substr($uuid, 9, 4), substr($uuid, 14, 4),
                                                        substr($uuid, 19, 2), substr($uuid, 21, 2))),
                                        array_map('hexdec',
                                                  str_split(substr($uuid, 24, 12), 2))));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文