|
1 |
| -jsstruct |
| 1 | + _ _ _ |
| 2 | + ___| |_ _ __ _ _ ___| |_ (_)___ |
| 3 | + / __| __| '__| | | |/ __| __| | / __| |
| 4 | + \__ \ |_| | | |_| | (__| |_ _ | \__ \ |
| 5 | + |___/\__|_| \__,_|\___|\__(_)/ |___/ |
| 6 | + |__/ |
| 7 | +Description |
2 | 8 | ========
|
3 | 9 |
|
4 |
| -The implementation of Python Struct to Javascript |
| 10 | +This is an implementation of Python Struct to Javascript. |
| 11 | + |
| 12 | +The idea is to make an easy interface like python struct in javascript to parse strings as C Types. |
| 13 | + |
| 14 | +What is done |
| 15 | +======== |
| 16 | +* Full unpack support. I implemented all types unpack from python struct |
| 17 | +* Big Endian and Little endian Support. You can choose the endianess like you do in python struct. |
| 18 | + |
| 19 | +TODO |
| 20 | +======= |
| 21 | +* Packing functions. |
| 22 | + |
| 23 | +How to use it |
| 24 | +======= |
| 25 | + |
| 26 | +In **python**, you use something like that for an int **1234**: |
| 27 | + |
| 28 | +```python |
| 29 | +import struct |
| 30 | +data = '\xd2\x04\x00\x00' |
| 31 | +struct.unpack("I", data) # This will return (1234,) |
| 32 | +``` |
| 33 | +So in **struct.js** you will do basicly the same: |
| 34 | +```javascript |
| 35 | +var data = '\xd2\x04\x00\x00'; |
| 36 | +struct.unpack("I", data); // This will return [1234] |
| 37 | +``` |
| 38 | + |
| 39 | +It works also for multiple packed data, in **python**: |
| 40 | +```python |
| 41 | +import struct |
| 42 | +data = '\xe0#\x00\x00\x00\x00(Aa' |
| 43 | +struct.unpack("Ifc", data) # This will return (9184, 10.5, 'a') |
| 44 | +``` |
| 45 | + |
| 46 | +In **struct.js**: |
| 47 | +```javascript |
| 48 | +var data = '\xe0#\x00\x00\x00\x00(Aa'; |
| 49 | +struct.unpack("Ifc", data); // This will return [9184, 10.5, "a"] |
| 50 | +``` |
| 51 | + |
| 52 | +The function syntax: |
| 53 | +======= |
| 54 | + |
| 55 | +```javascript |
| 56 | +struct.unpack(fmt, string) |
| 57 | +``` |
| 58 | + |
| 59 | +Arguments: `fmt` a string containing the types and endianess: |
| 60 | + |
| 61 | +First Character is endianess (Optional) |
| 62 | +* `@` Little Endian |
| 63 | +* `=` Little Endian |
| 64 | +* `<` Little Endian |
| 65 | +* `>` Big Endian |
| 66 | +* `!` Big Endian |
| 67 | + |
| 68 | +First and/or other characters as the format: |
| 69 | + |
| 70 | +* Format - C Type - Size - Description |
| 71 | +* `x` Pad Byte - 1 - This just skips one byte at the data |
| 72 | +* `c` char - 1 - String of Length 1 |
| 73 | +* `b` signed char - 1 - Integer |
| 74 | +* `B` unsigned char - 1 - Integer |
| 75 | +* `?` boolean - 1 - Boolean |
| 76 | +* `h` short int - 2 - Int |
| 77 | +* `H` unsigned short - 2 - Integer |
| 78 | +* `i` int - 4 - Integer |
| 79 | +* `I` unsigned int - 4 - Integer |
| 80 | +* `l` long integer - 4 - Integer |
| 81 | +* `L` unsigned long - 4 - Integer |
| 82 | +* `q` long long - 8 - Integer |
| 83 | +* `Q` unsigned long long - 8 - Integer |
| 84 | +* `f` float - 4 - Float |
| 85 | +* `d` double - 8 - Double |
| 86 | +* `s` char[] - ? - String |
| 87 | +* `p` char[] - ? - String |
| 88 | +* `P` void * - 4 - Integer |
| 89 | + |
| 90 | +Returns : array with the elements |
0 commit comments