doubi9999 2014-04-30 09:42
浏览 50
已采纳

如何在PHP中将多个十进制值转换为十六进制

Hi all i tried searching a bit but didnt found anything which will help me precisely please help me this is last thing which will help my program authentication system to get going i am trying to scan for users base ID , now BaseID is stored in my database i fetch it via sql and from there read it from my program ( made in Lua ) problem is i cant directly scan the BaseID its in 4byte format and gives too many results instead if i convert it in hex form it works well , i want to convert it in hex format at server using php so as soon as its fetched it gets converted in hex so i need help in conversion script i am unable to do it atm i use

$BaseID = ''
$BaseIDHex = dechex($BaseID);
{
   echo $BaseIDHex;
}

its does what i need partially

suppose my BaseID was 1234567890 it gives me Output as : 499602D2 but i want it in this form : D2 02 96 49 ( inverted and space in between )


Update : New question added in same one

$BaseID  = '';
$BaseID  = $row1['field_12'];
$Addition = ' 00 00 00 00 00 00 00 00 00"';
$BaseIDHex = dechex($BaseID);
$result = '';
while(strlen($BaseIDHex) >=2){
    $result = strtoupper($result.substr($BaseIDHex, -2))." ";
    $BaseIDHex = substr($BaseIDHex, 0, strlen($BaseIDHex)-2);
}
echo 'check = "';
echo trim($result);
echo $Addition;

it pritns me check = "B9 09 EE 00 00 00 00 00 00 00 00 00"

It works perfectly now i want to make some changes in the above code the $BaseID was 1 no. 15600057 but sometimes i have multiple ids stored in db like 15600057,12600057 any idea how i convert both of them into hexstring and echo them in the same way show in the above code ? so it echoes the final result like this

check = "B9 09 EE 00 00 00 00 00 00 00 00 00" check1 = "C9 56 fe 00 00 00 00 00 00 00 00 00" ??

  • 写回答

2条回答 默认 最新

  • 普通网友 2014-04-30 10:10
    关注

    @Shankar Damodaran 's code looks way cleaner(+1) but i'd still want to post my version

    $BaseID = '1234567890';
    $BaseIDHex = dechex($BaseID);
    $result = '';
    while(strlen($BaseIDHex) >=2){
        $result = strtoupper($result.substr($BaseIDHex, -2))." ";
        $BaseIDHex = substr($BaseIDHex, 0, strlen($BaseIDHex)-2);
    }
    echo trim($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度