?
Solved

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

Posted on 2014-09-27
7
Medium Priority
?
6,556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
Nas-Banov earned 2000 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 2000 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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

762 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