Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript equivalent to php unpack() function

I am looking for the javascript equivalent of php unpack() function? can someone direct me please. Thanks!

like image 442
jazz Avatar asked Sep 05 '11 08:09

jazz


2 Answers

Here is an unpack function for JS:

https://github.com/kvz/phpjs/blob/master/workbench/misc/unpack.js

like image 118
JoolzCheat Avatar answered Sep 23 '22 13:09

JoolzCheat


If nodejs (4.5/6.5) would be the environment, Buffer can partially achieve the functionality of unpack():

const buf = Buffer.from([0, 0, 0, 5]);
// Prints: 83886080
console.log(buf.readInt32LE());

See its documentation: https://nodejs.org/api/buffer.html#buffer_buf_readint32le_offset_noassert

This is equivalent to:

 unpack('V', join('', array_map(function ($a) { return chr($a); }, [0, 0, 0, 5])));
like image 37
Nobu Avatar answered Sep 23 '22 13:09

Nobu