doulang9521 2018-04-17 20:59
浏览 684
已采纳

PHP相当于Java中的getLeastSignificantBits()和getMostSignificantBits?

I'm working with UUIDs in PHP and I have to query a database that stores the most significant bits and least significant bits of a UUID in two separate columns. I found this question for Python, which seems like exactly what I'd need in PHP, but I don't know how to convert the code, and I've never been great with byte manipulation.

What would the equivalent functions for getLeastSignificantBits() and getMostSignificantBits() be in PHP? Thanks!

Edit: Example data (if helpful):

UUID: b33ac8a9-ae45-4120-bb6e-7537e271808e

...should convert to...

Upper Bits: -5531888561172430560
Lower Bits: -4940882858296115058
  • 写回答

3条回答 默认 最新

  • dqb14659 2018-05-03 03:58
    关注

    You simply need two libraries and bcmath extension, using

    composer require ramsey/uuid moontoast/math.

    Parse UUID using Ramsey\Uuid\Uuid:

    $uuid = \Ramsey\Uuid\Uuid::fromString('b33ac8a9-ae45-4120-bb6e-7537e271808e');
    echo 'Upper Bits: ' . $uuid->getMostSignificantBits() . "
    ";
    echo 'Lower Bits: ' . $uuid->getLeastSignificantBits() . "
    ";
    

    You get:

    Upper Bits: 12914855512537121056
    Lower Bits: 13505861215413436558
    

    Using those methods you get Moontoast\Math\BigNumber object, so you can get it's value or convert to different bases:

    $higher = $uuid->getMostSignificantBits();
    echo 'Upper Bits 10-base: ' . $higher->getValue() . "
    ";
    echo 'Upper Bits hex: ' . $higher->convertToBase(16) . "
    ";
    

    You get:

    Upper Bits 10-base: 12914855512537121056
    Upper Bits hex: b33ac8a9ae454120
    

    You can also use $uuid->getMostSignificantBitsHex() and $uuid->getLeastSignificantBitsHex() which are already converted to hex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证