How to convert a raw string r'\xdd' to '\xdd' more gracefully?
Jach Feng
jfong at ms4.hinet.net
Wed Dec 7 02:40:12 EST 2022
Thomas Passin 在 2022年12月7日 星期三中午12:51:32 [UTC+8] 的信中寫道:
> 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.
Thank you for reminding that the '0x'+ in the to1byte() definition is redundant:-)
Just not sure if there is a better way than using chr(int(...)) to do it.
Yes, for this specific case, slice is much simpler than re.sub().
More information about the Python-list
mailing list