javascript equivalent to php unpack() function
Asked Answered
N

2

6

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

Notwithstanding answered 5/9, 2011 at 8:22 Comment(1)
php.js has a pack() implementation but apparently no unpack() :-SFalstaffian
I
1

Here is an unpack function for JS:

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

Ifni answered 17/3, 2012 at 16:20 Comment(1)
The link is not available as of now. Here's a related issue and the direct link to the js file: github.com/kvz/locutus/blob/… It says it's not production ready.Distributary
D
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])));
Distributary answered 14/9, 2016 at 4:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.