2012-08-13 33 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驅動程序可能會將這些原始位返回,而不是轉換爲可讀的十六進制字符串本身,因此請在查詢中進行。