'How do I remove multiple 0x prefixes in one line of text?

So let's say I have this line of text 0x730x700x770x770x7a. As some of you might have noticed, those are multiple hex numbers bunched together. What I want is a way to remove the 0x prefix despite the position it is on the line of text. Take in mind that the above line of multiple hex values is just an example. My goal after removing the prefix is to put every different hex value inside a list and turn them into ASCII. Any advice would be appreciated because I don't have much knowledge with Python.



Solution 1:[1]

Don't remove the prefixes. Use built-in Python facilities to get your list of integers directly.

>>> a = "0x730x700x770x770x7a"
>>> list(int(a[i:i+4],16) for i in range(0,len(a),4))
[115, 112, 119, 119, 122]

Solution 2:[2]

Try the following:

num = "0x730x700x770x770x7a"
lst = num.split("0x")[1:]
asciilst = []

for x in lst:
  print(x)
  bytes_object = bytes.fromhex(x)
  ascii_string = bytes_object.decode("ASCII")
  asciilst.append(ascii_string)

print(asciilst)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BoarGules
Solution 2 Kayathiri Mahendrakumaran