Solved

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

Posted on 2014-09-27
7
3,209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:beavoid
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

743 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

18 Experts available now in Live!

Get 1:1 Help Now