Solved

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

Posted on 2014-09-27
7
4,564 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
looping 11 37
Reading text file in Python gives strange results 7 93
Python 3.5.2 - I need help with a script 8 47
Python filter object attributes 2 24
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 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 …

830 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