1

我有一个生成 UID 的代码:

        $time_low = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT);
        $time_mid = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT);

        $time_high_and_version = mt_rand(0, 255);
        $time_high_and_version = $time_high_and_version & hexdec('0f');
        $time_high_and_version = $time_high_and_version ^ hexdec('40');  // Sets the version number to 4 in the high byte
        $time_high_and_version = str_pad(dechex($time_high_and_version), 2, '0', STR_PAD_LEFT);

        $clock_seq_hi_and_reserved = mt_rand(0, 255);
        $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved & hexdec('3f');
        $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved ^ hexdec('80');  // Sets the variant for this GUID type to '10x'
        $clock_seq_hi_and_reserved = str_pad(dechex($clock_seq_hi_and_reserved), 2, '0', STR_PAD_LEFT);

        $clock_seq_low = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT);
        $node = str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT);
        $guid = $time_low . '-' . $time_mid . '-' . $time_high_and_version . $clock_seq_hi_and_reserved . '-' . $clock_seq_low . '-' . $node;

它生成一个字符串,如:011FFF33-CA4A-44E8-8CD5-7344D8E94344。当我从 MS SQL 2008 数据库中读取它时,我得到如下二进制字符串:3ÿJÊèDŒÕsDØéCD。如何将其读取为十六进制字符串而不是二进制字符串?谢谢!

4

1 回答 1

0

在 select 调用中将其转换为字符串 - uid 存储在原始位中,而不是您看到的漂亮的十六进制字符串。mssql 驱动程序可能会将这些原始位传回,并且本身不会转换为可读的十六进制字符串,因此请在查询中执行此操作。

于 2012-08-13T12:35:39.463 回答