6

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

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
jazz
  • 143
  • 1
  • 3
  • 8
  • php.js has a [`pack()` implementation](http://phpjs.org/functions/pack:880) but apparently no `unpack()` :-S – jensgram Sep 05 '11 at 08:29

2 Answers2

1

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])));
Nobu
  • 9,965
  • 4
  • 40
  • 47
1

Here is an unpack function for JS:

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

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
JoolzCheat
  • 261
  • 4
  • 8
  • 3
    The link is not available as of now. Here's [a related issue](https://github.com/kvz/locutus/issues/221) and the direct link to the js file: https://github.com/kvz/locutus/blob/98d264640c6a1586a1235d0f1da044c4c28a3752/workbench/misc/unpack.js It says it's not production ready. – Nobu Sep 14 '16 at 04:21