Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop22 authored May 18, 2022
1 parent f5e089d commit 94bc4ba
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,38 @@ pip install binread

## Usage

Just create a dictionary mapping for each field and it's type and construct a `Format` object, then pass some bytes to `Format.read`:
Just create a class with each field representing a certain data-type and decorate it with `format`, then pass some bytes to `<cls>.read()`:

```python
from binread import Format, U16, Array

from binread import Format, U16, Array, format

data = open('example.bin', 'rb').read()

@format
class Fmt:
field0 = U16,
field1 = Array(U16, length=5)

print(Fmt.read(data))
# Fmt(field0=16, field1=[1, 2, 3, 4, 5])
```
Or create a dictionary mapping for each field and it's type and construct a `Format` object.
```python

fmt = Format({
"field0": U16,
"field1": Array(U16, length=5)
})

result = fmt.read(open('example.bin', 'rb').read())
print(result)

print(fmt.read(data))
# {
# "field0": 16,
# "field1": [1, 2, 3, 4, 5]
# }
```

`Format.read` expects a `bytes` object and by default raises an exception if there are bytes left over. Set `allow_leftover=True` to disable this behaviour. If leftover bytes are allowed, the number of bytes that where read can be inspected with `return_bytes=True`.
`Format.read()` expects a `bytes` object and by default raises an exception if there are bytes left over. Set `allow_leftover=True` to disable this behaviour. If leftover bytes are allowed, the number of bytes that where read can be inspected with `return_bytes=True`.

### Arrays

Expand Down Expand Up @@ -111,4 +123,4 @@ Binread supports the following types
- Signed integers: `I8`, `I16`, `I32`, `I64`
- Unsigned Integers: `U8`, `U16`, `U32`, `U64`
- Floats: `F16`, `F32`, `F64`
- Misc: `Char`, `String`, `Bool`
- Misc: `Char`, `String`, `Bool`, `Tuple`

0 comments on commit 94bc4ba

Please sign in to comment.