在node.js中重写PHP代码

时间:2017-08-24 04:41:53

标签: javascript php node.js

我需要从PHP迁移到node.js,但遇到了一些麻烦。我尝试在节点中写一些东西。有人可以帮助并解释如何改善这一点。主要问题是如何将PHP类迁移到节点。现在结果是0.将PHP类编写为javaScript对象或者更好地将它们编写为函数是一个好主意吗?函数CrcResult必须返回一些东西吗?

class CrcResult {

public $Name = '';
public $Crc = 0;

}

 class CrcParams {

public $Name;
public $Array;
public $Init;
public $XorOut;
public $RefOut;
public $Poly;
public $RefIn;
public $Check;

}

$CRC_32_ = new CrcParams();
$CRC_32_->Init = 0xFFFFFFFF;
$CRC_32_->Name = "CRC-32";
$CRC_32_->XorOut = 0xFFFFFFFF;
$CRC_32_->RefOut = TRUE;

$CRC_32_->Poly = 0x4C11DB7;
$CRC_32_->RefIn = TRUE;
$CRC_32_->Check = 0xCBF43926;
$CRC_32_->Array = array(
  0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,.....);

class Crc32 {
public function ComputeCrc($crcParams, $data) {
$crc = $crcParams->Init;

if ($crcParams->RefOut) {
    foreach ($data as $d) {
        $crc = $crcParams->Array[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFFFFFF);
    }
} else {
    foreach ($data as $d) {
        $crc = $crcParams->Array[(($crc >> 24) ^ $d) & 0xFF] ^ ($crc << 8);
    }
}

$crc = $crc ^ $crcParams->XorOut;

$result = new CrcResult();
$result->Crc = $crc;

return $result;
}

}

$crcList = array($CRC_32_);    
$crc_algo = $CRC_32_;
$crc_calculator = new Crc32;

$crcResult = $crc_calculator->ComputeCrc($crc_algo, $output_data);
$calcdata = $crcResult->Crc & 0xFFFFFFFF;

$ar = unpack("C*", pack("L", $calcdata));

Bellow my node.js script

 function CrcResult() {
 var name = '';
 var Crc = 0;
 };

var CrcParams  = {};

CrcParams.Init = 0xFFFFFFFF;
CrcParams.Name = "CRC-32";
CrcParams.XorOut = 0xFFFFFFFF;
CrcParams.RefOut = true;
CrcParams.Poly = 0x4C11DB7;
CrcParams.RefIn = true;
CrcParams.Check = 0xCBF43926;
CrcParams.Array = [
    0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,....];


function ComputeCrc(crcParams, data) {
var crc = crcParams.Init;

if (crcParams.RefOut) {
  data.forEach(function(item, data) {
  crc = crcParams.Array[(item ^ crc) & 0xFF] ^ (crc >> 8 & 0xFFFFFF);
    });
} else {
    data.forEach(function(item, data) {
        crc = crcParams.Array[((crc >> 24) ^ item) & 0xFF] ^ (crc << 8);
    });
}

crc = crc ^ crcParams.XorOut;

var result = new CrcResult();
result.Crc = crc;

return result;
}


var crcList = CrcParams;    
var crc_algo = CrcParams;

0 个答案:

没有答案