0

I have a string named text which is the following:

'\x11@*\x00*\x00\x00\x00-\xa1\t\x00\xa3\x13\x00\x00\xff\x7f\x00\x00\x00\x00 \x00[logEvenTPlace:12] 3as\x

Because I had to decode it using:

(bytes.fromhex(text))

It has become an array b", which doesn't let me apply some basic regex rules.

I would like to convert, if its possible the value of my variable, literally without encoding or coding to a string type r".

Casting it as a string didnt work and if I do decode it crashes because some values are out of range.

Thanks.

Alejandro A
  • 1,150
  • 1
  • 9
  • 28
  • 1
    *It has become an array b", which doesn't let me apply some basic regex rules*: regexes can work fine on `bytes` objects, provided you use a bytes value for the regex pattern. – Martijn Pieters Feb 28 '19 at 12:09
  • 1
    There is no *string type r'..'*. The `r'...'` notation is just *an alternative syntax* to create string objects. `r'foo\bar'` and `'foo\\bar'` produce the same string value, just using different syntax. It's as if you are asking to turn the int value `42` into the expression value `23 + 19` instead. – Martijn Pieters Feb 28 '19 at 12:10
  • 1
    You don't need a raw string literal to use regexes on a bytes value, you only use those to [minimise the number of backslashes you have to use](https://docs.python.org/3/howto/regex.html#the-backslash-plague); `re.findall(rb'\[logEvenTPlace:(\d+)\]', your_sample_value)` produces `[b'12']`, for example, and I didn't need to use `\\[` and `\\d` and `\\]` in that expression. – Martijn Pieters Feb 28 '19 at 12:13
  • @MartijnPieters But If I want to capture everything from '[' to '3as' this won't work on a byte array:print(re.match(rb'\[(.*?)\\x00',decoded_msg)) – Alejandro A Feb 28 '19 at 12:35
  • 1
    Don't confuse the byte *representation* with the byte *value*, see the duplicate. You don't have the literal text *backslash*, *lowercase letter x*, *digit zero*, *digit zero* in that value, you have a null-bytes, integer value 0, represented by the escape sequence `\x00`. You want to match `rb'[(.*?)\x00'`, so *one* backslash, and you'll get the shortest piece from the input between `[` and the next null byte. Your input sample has no such section in it, however, but that could be because it looks incomplete. – Martijn Pieters Feb 28 '19 at 13:11
  • @MartijnPieters Managed to solve it, thanks again. – Alejandro A Feb 28 '19 at 13:23

0 Answers0