How can I read hexadecimal data with python?

2024/11/16 14:43:50

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:

        [Flags]public enum GameRobotCommands{reset = 0x0,turncenter = 0x1,turnright = 0x2,turnleft = 0x4,standstill = 0x8,moveforward = 0x10,movebackward = 0x20,utility1 = 0x40,utility2 = 0x80}

I'm doing this over TCP and got the TCP up and running, but can I plainly do this in Python to check flags:

if (self.data &= 0x2) == 0x2:#make the robot turn right code

Is there a way in python I can define the same enums that I have in c# (for higher code readability)?

Answer

Hexadecimal notation is just that, a way to write down integer numbers. You can enter 0x80 in your source code, or you can write it down as 128, it means the same thing to the computer.

Python supports the same integer literal syntax as C in that respect; list the same attributes on a class definition and you have the Python equivalent of your enum:

class GameRobotCommands(object):reset = 0x0turncenter = 0x1turnright = 0x2turnleft = 0x4standstill = 0x8moveforward = 0x10movebackward = 0x20utility1 = 0x40utility2 = 0x80

The C# application is probably sending these integers using the standard C byte representations, which you can either interpret using the struct module, or, if sent as single bytes, with ord():

>>> ord('\x80')
128
>>> import struct
>>> struct.unpack('B', '\x80')
(128,)
https://en.xdnf.cn/q/119518.html

Related Q&A

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…

Encryption code in def function to be written in python

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a …

Creating xml from MySQL query with Python and lxml

I am trying to use Python and LXML to create an XML file from a Mysql query result. Here is the format I want.<DATA><ROW><FIELD1>content</FIELD1><FIELD2>content</FIELD2…

How to add another iterator to nested loop in python without additional loop?

I am trying to add a date to my nested loop without creating another loop. End is my list of dates and end(len) is equal to len(year). Alternatively I can add the date to the dataframe (data1) is that …

How to know where the arrow ends in matplotlib quiver

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP

How to replace the column of dataframe based on priority order?

I have a dataframe as follows df["Annotations"] missense_variant&splice_region_variant stop_gained&splice_region_variant splice_acceptor_variant&coding_sequence_variant&intron…

Scraping from web page and reformatting to a calender file

Im trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940And Ive gotten as far (thanks to alecxe) as retrieving the date and teams.from scrapy.item import Item, Field fr…

Python Text to Data Frame with Specific Pattern

I am trying to convert a bunch of text files into a data frame using Pandas. Thanks to Stack Overflows amazing community, I almost got the desired output (OP: Python Text File to Data Frame with Specif…

Python Multiprocess OpenCV Webcam Get Request [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…