Sphen
Sphen

Reputation: 11

How to efficiently store an int into bytes?

I am developing a program that uses the python 3.7 socket library to send and receive information, and I want it to be efficient network wise (doesn't need to be all that fast, I don't have enough time to build it in C) meaning if I want to send the number 1024, I should only need 2 bytes.

In C, all that needs to be done is cast the int as a string, access the first 2 bytes and that is that (2 byte number to a 2 byte array/"string").

In Python, even when using the bytes structures, I always end up with something of the sort b'(\x8a', which is significantly larger than the needed byte amount.

The socket.send function only accepts byte objects, so I tried parsing the int as so (to_bytes and from_bytes), but ended up with an even bigger amount of bytes (b'(\x8a') than if I had used str()

When I set the receive buffer to 2 bytes for a number like 1024, I want to receive 1024 and not 10.

Upvotes: 0

Views: 892

Answers (1)

Draconis
Draconis

Reputation: 3491

If you want to pack numbers into bytes, you'll want the struct module.

>>> struct.pack('>H', 1024)
b'\x04\x00'

This packs the data 1024 into a single unsigned short aka uint16_t (H), in big-endian format (>). This gives the two bytes 04 00.

In practice, struct is usually used to pack a lot of data together, using longer format strings:

data = struct.pack('>bbbbHHiid', dat1, dat2, dat3...)

You probably see now why that module gets its name!

If you really only need to send a single number, you can use the built-in int.to_bytes method:

>>> (1024).to_bytes(2, 'big')
b'\x04\x00'

The first argument is the number of bytes to use, the second is the endianness.

Upvotes: 1

Related Questions