Python Forum
How do I mask Hex value wiht 0xFF?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I mask Hex value wiht 0xFF?
#1
Hello ,
I have this list
My List ['0x5b', '0x1ab', '0x282', '0x140', '0x6d7', '0x170']
0x5b<class 'str'>
0x1ab<class 'str'>
0x282<class 'str'>
0x140<class 'str'>
0x6d7<class 'str'>
0x170<class 'str'>
I want\need to maks all values with 0xFF

so the finall list will be
0x5b
0xab
0x82
0x40
0xd7
0x70
*********
there is no way to delete this post , I have found how to do it

while i < 6:
    temp= int(MyList[i], 16)
    temp = temp & int(0xff)
    MyList[i] = hex(temp)
    print(MyList[i])
    i += 1
Thanks,
Reply
#2
A couple ways you could do it. Since you're starting with strings, just treat them as strings. Print the last two letters of each.

Otherwise, you could convert the strings into a number, mask it off, then print the hex version of that number.

hex_in = ['0x5b', '0x1ab', '0x282', '0x140', '0x6d7', '0x170']

# string manipulation
for hex_string in hex_in:
    print(f"0x{hex_string[-2:]}")

print()

# numeric manipulation
for hex_string in hex_in:
    num = int(hex_string, 16)
    print(hex(num & 0xff))
Reply
#3
Convert to int first.
>>> hex(int('0x1ab', 0) & 255)
'0xab'
That said, I have the feeling that you should be working with bytes from the very beginning instead of str.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using boolean mask in Numpy for 3D IlikePi 0 1,471 Nov-14-2020, 10:08 PM
Last Post: IlikePi
  Saving a mask as a png file in opencv DanTheMan 1 5,604 Jan-31-2020, 09:20 AM
Last Post: ThiefOfTime
  Subnet Mask Ranges ab52 0 1,785 Mar-11-2019, 10:39 AM
Last Post: ab52
  Create a List for Boolean Mask. leoahum 4 3,799 Oct-09-2018, 02:38 PM
Last Post: leoahum
  python script to get wildcard mask output in the following format techrichit 0 3,779 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Mask superclass __init__ fig0 5 3,766 Jan-11-2018, 06:59 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020