将php代码重写为js

时间:2017-09-10 16:52:33

标签: javascript php

请帮帮我,在javascript上重写一遍。事实是,当使用第三方功能时,他们会返回另一个。

$from = '';
$to = '';
$width = 16;
if ($from === '') {
    for ($i = 0; $i <= 0xFF; $i++) {
        $from .= chr($i);
        $to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : ".";
    }
}
$hex = str_split(bin2hex($data), $width * 2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
$str = "";
foreach ($hex as $i => $line) {
    $str .= sprintf('%6X', $offset) . implode(' ', str_split($line, 2)) . ' ' . $chars[$i] . PHP_EOL;
    $offset += $width;
}

1 个答案:

答案 0 :(得分:0)

var from = '';
var to = '';
var width = '';

if (from === '') {
    for (var i = 0; i <= 0xFF; i++) {
        from += String.fromCharCode(i);
        to += (i >= 0x20 && i <= 0x7E) ? String.fromCharCode(i) : '.';
    }
}

var hex = [];
var chars = [];

var dataHex = parseInt(_DATA, 2).toString(16)

//Split dataHex into 'width * 2' sized chunks
for (var i = 0; i < dataHex.length; i += width * 2) {
    hex.push(dataHex.split(i, i + width*2));
}

var charsData = _DATA;

//Translate characters
for (var i = 0; i < charsData.length; i++) {
    if (from.indexOf(charsData[i]) > -1) {
        charsData[i] = to[from.indedOf(charsData[i])];
    }
}

//Split charsData into 'width' sized chunks
for (var i = 0, i < charsData.length; i += width) {
    chars.push(charsData.split(i, width));
}

var offset = 0;
var str = '';

for (var i = 0; i < hex.length; i++) {
    var line = hex[i];

    /*string formatting code goes here*/

    offset += width;
 }

这应该让你开始。

_DATA是您的php $data,无论它是什么。

代码未经测试,我没有足够的时间完成最后一个循环。您必须自己弄清楚字符串格式化部分,或者如果您仍然需要帮助而且明天没有其他人已经回答过此问题,我会测试并完成它:)