python - int to byte[] conversion, with 2 bytes?

Hi

I've queried parts of this issue before, but for 4 bytes to hold an integer value for a networking message byte[] encoding. - in Java

I think a 2 byte, intToBytes and BytesToInt system would be better for an RTS. using 4 bytes might be risky with UDP data loss issues and unnecessary size needs.

Would that hold enough of a range, in integer values, positive and negative, for RTS needs, e.g. port size, possible unit values?

Here is my 4 byte conversion code..
# byte array conversion methods

def intToBytes(n):
    b = bytearray([0, 0, 0, 0])   # init
    b[3] = n & 0xFF
    n >>= 8
    b[2] = n & 0xFF
    n >>= 8
    b[1] = n & 0xFF
    n >>= 8
    b[0] = n & 0xFF    
    
    # Return the result or as bytearray or as bytes (commented out)
    ##return bytes(b)  # uncomment if you need
    return b
    
    
def bytesToInt(b, offset):
    n = (b[offset+0]<<24) + (b[offset+1]<<16) + (b[offset+2]<<8) + b[offset+3]
    return n


###############################

Open in new window


what would such 2 byte python code look like?
Thanks
LVL 1
beavoidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nas-BanovCommented:
You should just use Python's module "struct", see https://docs.python.org/2/library/struct.html

It can handle packing/unpacking for you - and faster. You can specify what size ints and whether to do big-endian or little-endian (most all CPUs these days do little-endian but some internet formats are stuck in big-endian).
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x01\x00\x02\x00\x03\x00\x00\x00'

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
beavoidAuthor Commented:
That's cool, but I've seen this done with 2 bytes, mathematically. - Similar to my 4 byte code above, but just different & (and operator) values and << shift values.
Any ideas?
0
Nas-BanovCommented:
Not the right thing to do but if you insist:
def shortToBytes(n):
    b = bytearray([0, 0])   # init
    b[1] = n & 0xFF
    n >>= 8
    b[0] = n & 0xFF    
    return b
    
def bytesToShort(b, offset):
    n = (b[offset+0]<<8) + b[offset+1]
    return n

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

beavoidAuthor Commented:
Thanks
I was originally using 4 bytes for everything, and someone said 2 would be better for some integer components. Why is 4 best for all? Isn't UDP risky to have such a wide window for important integers? Lossy data transfers? What am I missing?
Thx
0
Nas-BanovCommented:
That is completely different question, for which there is not enough context here. And it is not Python related. Best is to ask the person that told you that, only they prolly know what they meant.

Wild guess? Theoretically, UDP datagram max size is above 65,500 bytes. However big datagrams will get chopped to smaller pieces during transmission (called "fragmentation") and then re-assembled on the other side. Probability of the datagram being lost increases proportionally (simplifying here: not exactly proportional but very close for our numbers) to the number of fragments, e.g. say likelihood of packet loss in certain network conditions is 2%, if it got chopped into 10 pieces - now you got 20% chance of not receiving it. (UDP is quick&dirty, it does not re-submit lost data - not to mention huge amounts of IP datagrams waiting for re-assembly will clog your IP stack)

So, what to do? Generally it is accepted If you can limit UDP payload size to 512 bytes, it won't be fragmented over internet (1500 on LAN). Now question is how much data per datagram will be packed - if nowhere close to 512 octets, no need to worry; otherwise using 2-byte shorts instead of 4-byte longs may allow you to pack 2x as many int-s.
0
beavoidAuthor Commented:
Thanks
Do you know if Blizzard RTS's use 512 UDP bytes or 1024?
I used big in my last RTS, bec I needed room for the unit numbers that were moving, esp large selections. - and it was on univ LAN
Do you think 65k x 65k will allow for a big enough playing field? I am not sure. Maybe I can send longs for movement co-ords
Also, how do I send the selected-units aspect along with the message? There could be thousands of units.
I keep the game state on the server. I can have unit groupings. If a non-grouped selection is being moved, I'd need more than a 512, message, prolly.
Thx
0
Nas-BanovCommented:
Ok, we are completely off-track here, please confirm answer and close question.
Try packet sniffer to see what Blizzard does. No idea about the rest, i am expert on Python - not games.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.