doutuo1939 2015-06-01 08:22
浏览 26
已采纳

将c ++函数转换为PHP?

I would like to convert my xor function to PHP. Can it be done? It needs to work as it`s working now...

string encryptDecrypt(string toEncrypt) {
    char key[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    string output = toEncrypt;
    for (int i = 0; i < toEncrypt.size(); i++)
        output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

    return output;
}
  • 写回答

2条回答 默认 最新

  • donglie1994 2015-06-01 08:34
    关注

    You can use almost the same syntax in PHP as your C++ function does:

    function encryptDecrypt($toEncrypt)
    {
        $key= array( '1', '2', '3', '4', '5', '6', '7', '8', '9' );
        $key_len = count($key);
        $output = $toEncrypt;
        for ($i = 0; $i < strlen($toEncrypt); $i++)
        {
           $output[$i] = $toEncrypt[$i] ^ $key[$i % $key_len];
        }
    
        return $output;
    }
    

    Online demo for C++ function: https://ideone.com/g9cpHJ

    Online demo for PHP function: https://ideone.com/3prgd0

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

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug