_ _ _
___| |_ _ __ _ _ ___| |_ (_)___
/ __| __| '__| | | |/ __| __| | / __|
\__ \ |_| | | |_| | (__| |_ _ | \__ \
|___/\__|_| \__,_|\___|\__(_)/ |___/
|__/
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
- Packing functions.
In python, you use something like that for an int 1234:
import struct
data = '\xd2\x04\x00\x00'
struct.unpack("I", data) # This will return (1234,)
So in struct.js you will do basicly the same:
var data = '\xd2\x04\x00\x00';
struct.unpack("I", data); // This will return [1234]
It works also for multiple packed data, in python:
import struct
data = '\xe0#\x00\x00\x00\x00(Aa'
struct.unpack("Ifc", data) # This will return (9184, 10.5, 'a')
In struct.js:
var data = '\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
x
Pad Byte - 1 - This just skips one byte at the datac
char - 1 - String of Length 1b
signed char - 1 - IntegerB
unsigned char - 1 - Integer?
boolean - 1 - Booleanh
short int - 2 - IntH
unsigned short - 2 - Integeri
int - 4 - IntegerI
unsigned int - 4 - Integerl
long integer - 4 - IntegerL
unsigned long - 4 - Integerq
long long - 8 - IntegerQ
unsigned long long - 8 - Integerf
float - 4 - Floatd
double - 8 - Doubles
char[] - ? - Stringp
char[] - ? - StringP
void * - 4 - Integer
Returns : array with the elements