douquan3294 2012-08-13 03:11
浏览 79
已采纳

PHP从MS sql读取二进制UID值为Hex

I have a code that generates 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;

It generates a string like: 011FFF33-CA4A-44E8-8CD5-7344D8E94344. When I read it from MS SQL 2008 database, I get binary string like: 3ÿJÊèDŒÕsDØéCD. How can I read it as hex string instead of binary string? Thanks!

  • 写回答

1条回答 默认 最新

  • duanbo1659 2012-08-13 12:35
    关注

    cast it to a string in the select call - a uid is stored in raw bits, not the nice hex string you see. the mssql driver is probably passing those raw bits back and not converting to a readable hex string itself, so do it in the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?