Solved

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

Posted on 2014-09-27
7
3,637 Views
Last Modified: 2014-09-29
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
0
Comment
Question by:beavoid
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
Nas-Banov earned 500 total points
ID: 40348198
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
 

Author Comment

by:beavoid
ID: 40348211
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
 
LVL 2

Assisted Solution

by:Nas-Banov
Nas-Banov earned 500 total points
ID: 40348422
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:beavoid
ID: 40348534
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
 
LVL 2

Expert Comment

by:Nas-Banov
ID: 40348750
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
 

Author Comment

by:beavoid
ID: 40348784
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
 
LVL 2

Expert Comment

by:Nas-Banov
ID: 40348826
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now