开发者

16 bytes binary form of canonical uuid representation in php

开发者 https://www.devze.com 2022-12-23 20:04 出处:网络
how can I get 16 bytes binary for开发者_JS百科m of the uuid from its string/canonical representation:

how can I get 16 bytes binary for开发者_JS百科m of the uuid from its string/canonical representation:

ex:1968ec4a-2a73-11df-9aca-00012e27a270

cheers, /Marcin


 $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.

0

精彩评论

暂无评论...
验证码 换一张
取 消