douzhuo1853 2017-01-04 05:42
浏览 864
已采纳

将Java方法Base64.encodeToString()转换为PHP

Hello I have the below java code:

Base64.encodeToString(("users name").getBytes(), 2);

And I'm trying to convert it to php:

bytesEnc("users name");

function bytesEnc($string) {
    $bytes = array();
    for($i = 0; $i < strlen($string); $i++){
        $bytes[] = ord($string[$i]);
    }
    return $bytes;
}

I converted getBytes() but I don't know what to do next. I know function base64_encode exists inn PHP but the second argument of encodetoString java method with value 2 bothers me. Please, help.

  • 写回答

1条回答 默认 最新

  • dongshou2017 2017-01-04 06:08
    关注

    According to the docs the second parameter is a bitwise flag.

    If the value is 2, then the flag NO_WRAP is used:

    Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).

    This means its removes the new lines.

    function bytesEnc($string){
      $string = str_replace(PHP_EOL, '', $string);
    
      $bytes = array();
      for($i = 0; $i < strlen($string) - 1; $i++){
        $bytes[] = ord($string[$i]);
      }
      return $bytes;
    }
    
    echo base64_encode(implode('',bytesEnc('users name')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算