_ _ _ ___| |_ _ __ _ _ ___| |_ (_)___ / __| __| '__| | | |/ __| __| | / __|\__ \ |_| | | |_| | (__| |_ _ | \__ \|___/\__|_| \__,_|\___|\__(_)/ |___/ |__/
This is an implementation of Python Struct to Javascript.
The idea is to make an easy interface like python struct in javascript to parse strings as C Types.
- Full unpack support. I implemented all types unpack from python struct
- Big Endian and Little endian Support. You can choose the endianess like you do in python struct.
- Make use of Typed Arrays, Array Buffer and DataView from HTML5 Spec
Inpython, you use something like that for an int1234:
importstructdata='\xd2\x04\x00\x00'struct.unpack("I",data)# This will return (1234,)So instruct.js you will do basicly the same:
vardata='\xd2\x04\x00\x00';struct.unpack("I",data);// This will return [1234]It works also for multiple packed data, inpython:
importstructdata='\xe0#\x00\x00\x00\x00(Aa'struct.unpack("Ifc",data)# This will return (9184, 10.5, 'a')Instruct.js:
vardata='\xe0#\x00\x00\x00\x00(Aa';struct.unpack("Ifc",data);// This will return [9184, 10.5, "a"]struct.unpack(fmt,string)
Arguments:fmt a string containing the types and endianess:
First Character is endianess (Optional)
@ Little Endian= Little Endian< Little Endian> Big Endian! Big Endian
First and/or other characters as the format:
- Format - C Type - Size - Description
xPad Byte - 1 - This just skips one byte at the datacchar -1 - String of Length 1bsigned char - 1 - IntegerBunsigned char - 1 - Integer?boolean - 1 - Booleanhshort int - 2 - IntHunsigned short - 2 - Integeriint - 4 - IntegerIunsigned int - 4 - Integerllonginteger - 4- IntegerLunsigned long - 4 - Integerqlong long - 8 - IntegerQunsigned long long- 8 - Integerffloat - 4 - Floatddouble - 8 - Doubleschar[] - ? - Stringpchar[] - ? - StringPvoid * - 4 - Integer
Returns : array with the elements