int to binary python

2024/10/11 14:25:37

This question is probably very easy for most of you, but i cant find the answer so far.

I'm building a network packet generator that goes like this:

class PacketHeader(Packet):
fields = OrderedDict([("head", "\x00"),("headpad", "\x00"),("headlen", "\x1a\x00" ),("presentflag", "\x2e"),("timestamp", "\x43\x10\x58\x16\xa1\x01\x00\x00"),("flag", "\x03"),("Sequencenum", "\x04\x00"),])

Later, I call my class and send the packet like this:

send(PacketHeader(Sequencenum=257))
send(PacketHeader(Sequencenum=258))
send(PacketHeader(Sequencenum=259))

Unfortunatly, on the wire i have \x32\x35\x37 (257 in hex) instead of having this :\x01\x01.

What i would like to do, is being able to:

send(PacketHeader(Sequencenum=257+1)) 

and have it converted to int correctly on the wire

Thanks in advance !

Answer

If you want to convert int or any other primitive data type to binary then the struct module might help you.

http://docs.python.org/library/struct

>>> import struct
>>> struct.pack('>i', 257)
'\x00\x00\x01\x01'
>>> struct.pack('<i', 257)
'\x01\x01\x00\x00'
>>> struct.unpack('<i', '\x01\x01\x00\x00')
(257,)
>>> struct.unpack('>i', '\x00\x00\x01\x01')
(257,)

ms4py mentioned you might want to convert 2 byte ints so:

>>> struct.pack('>h', 257)
'\x01\x01'
>>> struct.unpack('>h', '\x01\x01')
(257,)
>>> 

oh and:

>>> struct.unpack('>h', '\x10\x00')
(4096,)
>>> 
https://en.xdnf.cn/q/118313.html

Related Q&A

Get aiohttp results as string

Im trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio…

Waiting for a timer to terminate before continuing running the code

The following code updates the text of a button every second after the START button was pressed. The intended functionality is for the code to wait until the timer has stopped before continuing on with…

PySpark: how to resolve path of a resource file present inside the dependency zip file

I have a mapPartitions on an RDD and within each partition, a resource file has to be opened. This module that contains the method invoked by mapPartitions and the resource file is passed on to each ex…

Convert normal Python script to REST API

Here I have an excel to pdf conversion script. How can I modify it to act as a REST API? import os import comtypes.client SOURCE_DIR = D:/projects/python TARGET_DIR = D:/projects/python app = comtypes…

How to track changes in specific registry key or file with Python? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

How to use Android NDK to compile Numpy as .so?

Because the Numpy isnt a static library(it contains .py files, .pyc files, .so files, etc), so if I want to import it to my python code which is used in an Android phone(using CLE), I should recompile …

passing boolean function to if-condition in python

I"m learning python, and Im trying to do this, which I thought should be trivial, but apparently, its not. $python >>> def isTrue(data): ... "ping" in data ... >>>…

Unsupported operand type(s) for str and str. Python

Ive got the IF statement;if contactstring == "[Practice Address Not Available]" | contactstring == "[]":Im not sure what is going wrong(possibly the " "s?) but I keep ge…

getting Monday , june 5 , 2016 instead of June 5 ,2016 using DateTimeField

I have an app using Django an my my model has the following field: date = models.DateTimeField(auto_now_add=True,auto_now=False)Using that I get this: June 5, 2016, 9:16 p.m.but I need something like…

WeasyPrint usage with Python 3.x on Windows

I cant seem to get WeasyPrint to work on Windows with Python 3.4 or 3.5. Has anyone been able to do this? There arent forums at weasyprint.org and the IRC channel is dead. Ive been able to install …