2017-05-16 2 views
-5

Zuerst werde ich Ihnen sagen Ich kann kein Englisch sprechen, aber ich möchte versuchen, Informationen darüber, wie ein Perl-Paket in Javascript implementieren, um ein node.js-Modul zu machen.Javascript Perl Pack

Um dies zu tun, würde ich gerne mehr Informationen über das Perl-Paket erhalten. Insbesondere möchte ich vor allem die Information "C, H *, N" wissen.

Auch wenn ich mehr Informationen über Jspack bekommen könnte, wäre das wunderbar.

Vielen Dank im Voraus.

日本語(Japan)

+0

[perldoc -f pack] (http://perldoc.perl.org/functions/pack.html) –

+1

Es gibt Tutorials besonders entworfen, um Informationen über verschiedene Sprachen und mehr, insbesondere die Dokumentation von den Machern zur Verfügung zu stellen. SO ist keine Tutorialseite, bitte versuchen Sie etwas zu fragen, von dem viele Leute profitieren können. – Zeke

Antwort

0

pack 'C', pack 'N'pack 'H*' und werden verwendet, um eine Folge von Bytes zu erzeugen.

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; 
} 
+0

Warum der Downvote? – ikegami

+0

Was ist los in diesen Tagen, aber upvoeded, um das wettzumachen. –

+0

Ich habe versucht, den Code in Referenz zu testen. Dank Ihnen wurde das Problem gelöst. Vielen Dank. "Test-URL": http: //plnkr.co/urgQPFBQ76VSJyuqJy5f –