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

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 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch