16 bytes binary form of canonical uuid representation in php

三世轮回 提交于 2019-11-30 20:42:19
 $bin = pack("h*", str_replace('-', '', $guid));

pack

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!