On 12/6/2022 9:23 PM, Jach Feng wrote:
s0 = r'\x0a'
At this moment it was done by

     def to1byte(matchobj):
     ....return chr(int('0x' + matchobj.group(1), 16))
     s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0)

But, is it that difficult on doing this simple thing?

--Jach

I'm not totally clear on what you are trying to do here.  But:

s1 = r'\xdd'  # s1[2:] = 'dd'
n1 = int(s1[2:], 16)  # = 221 decimal or 0xdd in hex
# So
chr(n1) == 'Ý'  # True
# and
'\xdd' == 'Ý' # True

So the conversion you want seems to be chr(int(s1[2:], 16)).

Of course, this will only work if the input string is exactly four characters long, and the first two characters are r'\x', and the remaining two characters are going to be a hex string representation of a number small enough to fit into a byte.

If you know for sure that will be the case, then the conversion above seems to be about as simple as it could be. If those conditions may not always be met, then you need to work out exactly what strings you may need to convert, and what they should be converted to.


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to