# Combine byte values to a 32bib int

**URL:** <https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733>\
**Category:** Python Help\
**Created:** [January 20, 2025, 11:20am UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733 "2025-01-20T11:20:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![muekno](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/muekno/32/26317_2.png) [@muekno](https://discuss.python.org/u/muekno)\
**Post date:** [January 20, 2025, 11:20am UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733/1 "2025-01-20T11:20:07Z")

</div>

I get 4 8bit (1 Byte) values from a i.e. serial input. In fact it is unix time in pieces, but to work with it I need it in one 32 bit int.

Tried something with shift \>\> and \<\< but got not what I want  
Any help or hint Thank you

Rainer

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [January 20, 2025, 12:01pm UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733/2 "2025-01-20T12:01:56Z")

</div>

One way to do it is to simply concatenate the bytes and then convert.

Let’s say that you got something like `now_bytes` from a serial connection:

```python
import time

now = int(time.time())
print(now) # E.g. 1737373766
now_bytes = list(now.to_bytes(length=4, byteorder="big"))
print(now_bytes) # E.g. [103, 142, 56, 70]

```

Then you can do this:

```python
timestamp = int.from_bytes(bytes(now_bytes), byteorder="big")
print(timestamp) # E.g. 1737373766

```

The above assumes the serialized timestamp was received MSB first, aka. big endian.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [January 20, 2025, 12:43pm UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733/3 "2025-01-20T12:43:53Z")

</div>

Whether it’s a signed (`'i'` / int32 ) or unsigned(`'I'` / uint32) integer was not specified, but try:

```python
struct.unpack('i', ...)

```

> **[struct — Interpret bytes as packed binary data](https://docs.python.org/3/library/struct.html)**
>
> Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [January 20, 2025, 12:57pm UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733/4 "2025-01-20T12:57:37Z")

</div>

If the data is a Unix timestamp, signedness doesn’t matter if it corresponds to a date in the range 1970-01-01 - 2038-01-18.

You need to be careful with endianness, though. You should be explicit about endianness, especially when working with data received over a network.

To specify e.g. big endian data in the struct module, use `">"` in the format string: `struct.unpack(">i", ...)`.

---

<div class="post-metadata">

**Author:** ![muekno](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/muekno/32/26317_2.png) [@muekno](https://discuss.python.org/u/muekno)\
**Post date:** [January 20, 2025, 1:45pm UTC](https://discuss.python.org/t/combine-byte-values-to-a-32bib-int/77733/5 "2025-01-20T13:45:58Z")

</div>

Perfect thank you

```python
# code receiving data1, data2, data3, data4
now_bytes = [data1,data2,data3,data4]
print(now_bytes)
timestamp = int.from_bytes(bytes(now_bytes), byteorder="big")
print(timestamp)

```
