JavaScript函数的脚本失败到PHP

时间:2015-04-21 20:34:13

标签: javascript php

如何为PHP转录这些功能?我躺在PHP中,不知道应该替换哪些功能,有人帮助我吗?

JavaScript代码

function bytesToHex (bytes) {
  bytes = bytes || [];
  var arr = [];
  for (var i = 0; i < bytes.length; i++) {
    arr.push((bytes[i] < 16 ? '0' : '') + (bytes[i] || 0).toString(16));
  }
  return arr.join('');
}

function bytesFromHex (hexString) {
  var len = hexString.length,
      i,
      start = 0,
      bytes = [];

  if (hexString.length % 2) {
    bytes.push(parseInt(hexString.charAt(0), 16));
    start++;
  }

  for (i = start; i < len; i += 2) {
    bytes.push(parseInt(hexString.substr(i, 2), 16));
  }

  return bytes;
}

我试图自己做但没有成功。请参阅我的尝试代码:

PHP代码:

function bytesToHex ($bytes) {
  $bytes = $bytes || new array();
  $arr = new array();

  for($i = 0; $i < sizeof($bytes); $i++) {
    array_push(($bytes[$i] < 16 ? '0' : '') + ($bytes[$i] || 0).toString(16));
  }
  return arr.join('');
}

function bytesFromHex ($hexString) {
  $len = strlen(hexString);
  $start = 0;
  $bytes = new array();

  if (strlen($hexString) % 2) {
    $bytes = array_push(intval(hexString.charAt(0), 16));
    $start++;
  }

  for ($i = $start; i < $len; $i += 2) {
    $bytes = array_push(intval(hexString.substr($i, 2), 16));
  }

  return bytes;
}

1 个答案:

答案 0 :(得分:2)

  

string base_convert(string $ number,int $ frombase,int $ tobase)

示例:

echo base_convert('A747', 16, 2);