> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/conversions/bytes.md).

# Bytes

Python 3 supports two types of binary sequence types:

* #### [Bytes Objects](https://docs.python.org/3/library/stdtypes.html#bytes-objects)

  Immutable sequences of single bytes
* #### [Bytearray Objects](https://docs.python.org/3/library/stdtypes.html#bytearray-objects)

  Mutable counterpart to bytes objects

There is no dedicated literal syntax for bytearray objects, instead they are always created by calling the constructor:

* Creating an empty instance: `bytearray()`
* Creating a zero-filled instance with a given length: `bytearray(10)`
* From an iterable of integers: `bytearray(range(20))`
* Copying existing binary data via the buffer protocol: `bytearray(b'Hi!')`

```python
>>> bytearray(5)
bytearray(b'\x00\x00\x00\x00\x00')
>>> bytearray(range(5))
bytearray(b'\x00\x01\x02\x03\x04')
```

## Conversion from and to Hex-strings

### Standard Python 3

In standard Python 3 you can convert from hexadecimal strings with the [fromhex()](https://docs.python.org/3/library/stdtypes.html#bytearray.fromhex) method

```python
>>> bytes.fromhex('63727970746f')
b'crypto'
>>> bytes.fromhex('63 72 79 70 74 6f')
b'crypto'
>>> bytearray.fromhex('63727970746f')
bytearray(b'crypto')
```

Note that spaces between the bytes are supported.

Conversion to hex-strings can be done with the [hex()](https://docs.python.org/3/library/stdtypes.html#bytearray.hex) method

```python
>>> b'ABC123'.hex()
'414243313233'
>>> b'ABC123'.hex(' ')
'41 42 43 31 32 33'
>>> b'ABC123'.hex(':')
'41:42:43:31:32:33'
>>> b'ABC123'.hex(' ', 2)
'4142 4331 3233'
```

Note the support for optional parameters: `hex([sep[, bytes_per_sep]])`

### PwnTools

<mark style="background-color:yellow;">To Do here!!!</mark>

<mark style="background-color:yellow;">unhex()</mark>

## Conversion from and to Integers

Python 3 has two built-in methods that converts integers to/from bytes:

* [int.from\_bytes()](https://docs.python.org/3/library/stdtypes.html#int.from_bytes)
* [int.to\_bytes()](https://docs.python.org/3/library/stdtypes.html#int.to_bytes)

```python
>>> int.from_bytes(b'\x00\x10', byteorder='big')
16
>>> int.from_bytes(b'\x00\x10', byteorder='little')
4096
>>> i = 255
>>> i.to_bytes()
b'\xff'
>>> i = 65535
>>> i.to_bytes()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too big to convert
>>> i.to_bytes(2)
b'\xff\xff'
>>> i.to_bytes(4)
b'\x00\x00\xff\xff'
>>> i.to_bytes(4, byteorder='big')
b'\x00\x00\xff\xff'
>>> i.to_bytes(4, byteorder='little')
b'\xff\xff\x00\x00'
```

PyCryptodome supports conversion from and to long integers:

* [bytes\_to\_long()](https://www.pycryptodome.org/src/util/util#Crypto.Util.number.bytes_to_long)
* [long\_to\_bytes()](https://www.pycryptodome.org/src/util/util#Crypto.Util.number.long_to_bytes)

```python
>>> from Crypto.Util.number import *
>>> long_to_bytes(65535)
b'\xff\xff'
>>> long_to_bytes(3405691582)
b'\xca\xfe\xba\xbe'
>>> msg = 11515195063862318899931685488813747395775516287289682636499965282714637259206269
>>> long_to_bytes(msg)
b'crypto{3nc0d1n6_4ll_7h3_w4y_d0wn}'
>>> bytes_to_long(b'\xca\xfe\xba\xbe')
3405691582
```
