0

I have raw hex from a com port that I want to represent as a hex value that I can use later for bitwise operations.

For example, I have 1035 and I want it as 0x1035.

I am relatively certain the hex() function is not what I want. This converts int(1035) into hex 40B.

I have tried concatenating a '0x' to my value, the problem I found with this is it is then type str, which I can't do bitwise &,|, etc. Converting it to int results in: invalid literal for int() with base 10: '0x1035'

I have tried converting the hex to binary, with the same problem as before with type str and not type int.

Life becomes way easier when I define, hex = 0x1035, for example. But I want this to be reusable for many messages from the com port.

How do I convert a string '0x1035' to a hex int? Or, how do I tell the interpreter that the int I have, 1035, I want interpreted as hex, 0x1035?

3
  • Does this answer your question? Convert hex to binary Commented Mar 14, 2020 at 2:45
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: How to Ask, help center, meta.stackoverflow.com/questions/261592/…. Commented Mar 14, 2020 at 4:13
  • I understand your question as I needed the same thing. If you have a hex in str type, convert it to int. Python will handle it like hex in int type: i.e. int(hex_value_str, 16). Then, you can also do bit operations with this outcome as you do with hex int type. Commented Mar 18, 2022 at 10:17

1 Answer 1

0

Some good comments above that point to good references. 2 things worth mentioning. When you say you "have raw hex from a port" I'm assuming that when you ingest these numbers into python that the data type is a string that represents a hexadecimal value. If that is the case, you can just convert it

In [17]: input_data = ['10', '1035', 'FF', 'A']                                 

In [18]: hex_data = [int(t, 16) for t in input_data]                            

In [19]: hex_data                                                               
Out[19]: [16, 4149, 255, 10]

Now that python is storing these things as integer values, you can do bitwise operations on them, which is executed in the base-2 representation, obviously. Then you can express the output however you like. Same underlying number is in memory...python can express it in many bases.

In [20]: d1 = 'A'                                                               

In [21]: d2 = '3'                                                               

In [22]: d1 = int(d1, 16)                                                       

In [23]: d2 = int(d2, 16)                                                       

In [24]: d1                                                                     
Out[24]: 10

In [25]: d2                                                                     
Out[25]: 3

In [26]: d1&d2                                                                  
Out[26]: 2

In [27]: # let's check...                                                       

In [28]: bin(d1)                                                                
Out[28]: '0b1010'

In [29]: bin(d2)                                                                
Out[29]: '0b11'

In [30]: # looks right.....  (it is)    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.