Questions tagged [struct.pack]
34 questions
4
votes
1 answer
Sending Login Packet to Minecraft Server in Python Not Working
I have the following script in Python. What it does is tries to connect to a MineCraft server, first by sending a 'handshake', then sending a login request. The protocol specs can be found here: http://wiki.vg/Protocol
Anyway, the python script…

Jack
- 740
- 5
- 21
3
votes
1 answer
Does struct.pack(f'{len(x)}s', x) do anything at all?
Consider the following code:
import struct
x = b'example' # can be any bytes object
y = struct.pack(f'{len(x)}s', x)
print(x == y)
If I understand the documentation correctly, the function call will return the binary representation of a struct…

Ground
- 43
- 5
3
votes
0 answers
Is there a equivalent of python's struct.pack in C#?
Is there a more or less equivalent of Pythons' struct pack in C# ?
What i basically need to do is 'rewriting' a part of a python-script in C#. The following functions are required to prepare some text, so that it can be send via a socket...
def…

ThexBasic
- 715
- 1
- 6
- 24
3
votes
1 answer
Extracting strings from struct.pack
I need to convert an integer into a series of byte strings, and it looks like struct.pack is the best way to do that. One line gets me all the information I need:
In [51]: struct.pack("@L",1000)
Out[51]: '\xe8\x03\x00\x00'
My issue is that I…

Chris
- 9,603
- 15
- 46
- 67
2
votes
1 answer
Python struct.pack() data range error
I'm using python2.7 and I have this code. Data values are range from 0 to 65792.
data_length=30
code=202
data=[51400,31400,100,51400,31400,100,51400,31400,100]
checksum = 0
total_data = ['$', 'M', '<', data_length, code] + data
for i in…

Mine
- 23
- 1
- 1
- 3
2
votes
1 answer
python3 struct.pack with strings as parameters
i'm actually wondering about python3's struct.pack behaviour, but maybe i missed something.
i'm submitting an .jpg file via UDP. Btw: the socket.sendTo() and sendAll() function throws a "Python IOError: [Errno 90] Message too long" when i try to…

bugpulver
- 33
- 1
- 6
2
votes
1 answer
Getting IP address of the beagleboard using python
SIOCGIFADDR = 0x8915
def getIpAddr(iface = 'eth0'):
ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00'*14)
try:
res = fcntl.ioctl(sock, SIOCGIFADDR, ifreq)
except:
return None
ip =…

Andy
- 33
- 5
2
votes
3 answers
Printing out packing result from struct.pack
struct.pack returns packed result from input value.
In [19]: pack("i",4)
Out[19]: '\x04\x00\x00\x00'
I'm trying to printout the packed result as follows:
val = pack("i", 4)
print "%d" % int(val[0])
However, I got ValueError:
ValueError: invalid…

prosseek
- 182,215
- 215
- 566
- 871
1
vote
1 answer
Writing a large list of short integers with python
I have a large list of values, which I want written to a file, as short integers.
I want to do this efficiently, and in Python3.
One value at a time, is possible using the struct module:
struct.pack( "h", val )
It is possible to prefix the format…

Bram
- 7,440
- 3
- 52
- 94
1
vote
1 answer
struct.pack and struct.unpack only doing the first character of a string
So I'm trying to pack a packet header, and things are working fine, except for header flags I need to pack as strings are only unpacking the first character of the string.
For instance,
string = "ahhhhhh"
buffer = pack("s",…

decheftw
- 69
- 6
1
vote
1 answer
Meaning of struct.pack( ) arguments in code for Beagleboard IP assignment
I was writing a program to assign IP address to beaglebone black in python using ioctl.
(reference How to assign IP address to interface in python? and Getting IP address of the beagleboard using python )
Since IOCTL method implemented in Linux…
user6108870
1
vote
1 answer
struct.pack in Python3 got struct.error: required argument is not an integer
I'm doing a zipping func of this:
open_file = open(file_path,'rb')
open_save = open(save_path,'wb+')
try:
open_read = open_file.read()
data = zip_data(open_read)
head_xis = b'XIS'
head_version = bytes(0)
head_type1 = bytes(1)
…

Mapz.C
- 11
- 1
- 2
1
vote
1 answer
struct.pack() failing with int > 127
Why is
struct.pack("!bbbb", 0x2, r, g, b)
failing in my python code when r, g, or b is > 127?
I know that the "b" means the size of a given value is 1 byte according to the struct docs, but why does it fail with values over 127?

Adam Johns
- 35,397
- 25
- 123
- 176
1
vote
1 answer
python struct pack double
I want to convert -123.456 into a C double for network transmission in python. So I tried this:
struct.pack('d', -123.456)
I get this as a result:
'w\xbe\x9f\x1a/\xdd^\xc0'
Obviously there is some hex in there, but what is with the w, /, and ^…

Jake
- 2,515
- 5
- 26
- 41
1
vote
2 answers
python: integer out of range for 'L' format code
In python, the code is the following
envimsg = struct.pack("!LHL", 1, 0, int(jsonmsg["flow_id"], 16)) + \
struct.pack("!HQH", 1, int(flow["src id"],16), 0) + \
struct.pack("!HQH", 1, int(flow["dst id"],16), int(flow["dst…

jaeyong
- 8,951
- 14
- 50
- 63