Home > OS >  How can I overcome Python max byte range
How can I overcome Python max byte range

Time:11-12

I'm having a problem with sending bytearray to the socket. I'm trying to log into game server and I need to specify game version using bytes. This is what I have:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 10000))
s.connect(("127.0.0.1", 25565))

handshake = bytearray([6,0,340,0,0,69,2]) #2nd index specifies the game version (340)
s.send(handshake)
#[...]

The problem is that I get this error:

Traceback (most recent call last):
  File "c:\Users\Leon\Desktop\mc\mc_protocol.py", line 11, in <module>
    handshake = bytearray([6,0,340,0,0,69,2])
ValueError: byte must be in range(0, 256)

So is there any way to send byte to the socket higher than 256? Thanks.

CodePudding user response:

A byte only goes up to 255 - 8 binary digits can go from 00000000 up to 11111111. If you think you need to put the number 340 - binary 101010100 - in a byte then I don't know what to tell you. It doesn't fit. It is like asking how to write the number 567 in 2 digits.

If you're sure that 340 is the right number, then it probably needs to be split up into two bytes: 101010100 -> 1 01010100 -> 00000001 01010100 or possibly 01010100 00000001.

  • Related