2012-08-13 28 views
1

我有生成UID代码:PHP从MS SQL读取二进制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。我如何读取它作为十六进制字符串而不是二进制字符串?谢谢!

+0

将其转换为select调用中的字符串 - 存储的uid是原始位,而不是您看到的很好的十六进制字符串。 mssql驱动程序可能会将这些原始位返回,而不是转换为可读的十六进制字符串本身,因此请在查询中进行。 – 2012-08-13 03:35:46

+0

非常感谢,解决了我的问题!我做了“CAST(Id as varchar(20))”请更改您的评论以回答,我会接受它。 – Kristian 2012-08-13 09:22:08

回答

0

将它转换为select调用中的字符串 - 一个uid存储在原始位中,而不是您看到的很好的十六进制字符串。 mssql驱动程序可能会将这些原始位返回,而不是转换为可读的十六进制字符串本身,因此请在查询中进行。