dsf1222 2013-08-08 20:56
浏览 62
已采纳

C#Bitwise操作系统与PHP

I'm trying to convert some C# code into PHP.

The C# code is as follows:

strRequest="\auth\\pid\3045\ch\ehCkPNGSesp\1529559b837decb2e0ed2f576806f10aeda4d9cdf781acd3e53ad1c3b11a20684f131229\ip\16777343\skey\3759eqproof\1";
byte[] sendBuffer = Encoding.ASCII.GetBytes(strRequest);
Encode(sendBuffer, strRequest.Length);

private static void Encode(byte[] buff, int size){
        byte[] tmp = new byte[8];
        tmp[0] = 103;
        tmp[1] = 97;
        tmp[2] = 109;
        tmp[3] = 101;
        tmp[4] = 115;
        tmp[5] = 112;
        tmp[6] = 121;
        tmp[7] = 0;

        if (size != 0){
            int i = size;
            int j = 0;
            int k = 0;
            do{
                if (tmp[j] == 0)
                    j = 0;

                buff[k++] ^= tmp[j++];
                --i;
            } while (i != 0);
        }
}

Which should output a byte array of: '59','0','24','17','27','44','37','23','8','9','57','64','64','77','82','61','14','13','47','21','17','36','10','61','43','52','35','37','21','4','30','21','47','65','76','85','88','88','80','74','18','65','84','86','9','0','16','18','75','2','81','8','1','65','22','76','80','87','85','85','69','22','72','87','0','8','1','18','68','29','94','2','9','3','68','72','72','6','2','9','86','22','69','74','6','5','92','6','64','18','72','86','0','95','85','69','72','77','1','80','94','84','65','66','64','59','8','29','57','66','70','78','80','86','94','81','64','44','10','12','4','20','57','64','71','76','94','61','31','0','2','0','11','8','14','11','57','66',

My equivalent PHP code is as follows:

<?php

$strReq="\\auth\\\\pid\\3045\\ch\\atGzgVH5\esp\\1529559b837decb2e0ed2f576806f10ac7d8b28205dcbd64c59666a11d43b814024837cb\\ip\\16777343\\skey\\1292\eqproof\\1";

$strReqLen = strlen($strReq);

$strHex = unpack("C*",$strReq);

$test = Encode($strHex,$strReqLen);

print_r ($test);

function Encode($buff, $size){

$tmp = array(103,97,109,101,115,112,121,0);

$tester = array();
if ($size != 0){
$i = $size;
$j = 0;
$k = 0;

do {
    if($tmp[$j] == 0){
        $j = 0;
    }       
    $buff[$k++] ^= $tmp[$j++];
    --$i;
} while ($i != 0);
}
return $buff;
}

?>

But does not output anything like what the C# code does.

Any help greatly appreciated.

Thanks

Dave

  • 写回答

1条回答 默认 最新

  • doutui6241 2013-08-09 15:27
    关注

    C# uses zero-based arrays but PHP's unpack(..) uses a 1-based array.

    You could either change

    $strHex = unpack("C*",$strReq);
    

    to

    $strHex = array_merge(unpack("C*",$strReq));
    

    to make your PHP array zero-based, or simply set $k to an initial value of 1.

    After testing either solution the PHP results are the same as the C# output.

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

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置