javascript Perl包

时间:2017-05-16 15:41:09

标签: javascript perl pack

首先,我会告诉你 我不会说英语,但我想尝试获取有关如何在Javascript中实现Perl包以创建node.js模块的信息。

为此,我想获得有关Perl包的更多信息。特别是,我特别想知道的信息" C,H *,N"。

另外,如果我能获得有关jspack的更多信息,那就太棒了。

提前谢谢。

日本語(Japan)

1 个答案:

答案 0 :(得分:0)

pack 'C'pack 'N'pack 'H*'用于创建字节序列。

my $bytes = pack('C', $uint8);

# Array of bytes
var bytes = [];
bytes.push(uint8);

# String of bytes
var bytes = "";
bytes += String.fromCharCode(uint8);

my $bytes = pack('N', $uint32);

# Array of bytes
var bytes = [];
bytes.push((uint32 >> 24) & 0xFF);
bytes.push((uint32 >> 16) & 0xFF);
bytes.push((uint32 >>  8) & 0xFF);
bytes.push((uint32      ) & 0xFF);

# String of bytes
var bytes = "";
bytes += String.fromCharCode((uint32 >> 24) & 0xFF);
bytes += String.fromCharCode((uint32 >> 16) & 0xFF);
bytes += String.fromCharCode((uint32 >>  8) & 0xFF);
bytes += String.fromCharCode((uint32      ) & 0xFF);

my $bytes = pack('H*', $hex_str);

# Array of bytes
function hexToBytes(hex) {
    var bytes = [];
    for (var c = 0; c < hex.length; c += 2)
       bytes.push(parseInt(hex.substr(c, 2), 16));

    return bytes;
}

# String of bytes
function hexToBytes(hex) {
    var bytes = "";
    for (var c = 0; c < hex.length; c += 2)
       bytes += String.fromCharCode(parseInt(hex.substr(c, 2), 16));

    return bytes;
}