-1

I have this byte represented as string:

b'google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs'

I want to convert it to a raw string looking like this:

r'"google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs"

(without the r' displaying on printing)

I tried it with replace but can't figure out to get it to work. Maybe someone can help me here.

Greetings

Edit full code:

def decode_txt_rdata(rdata, rdlen):
    """decode TXT RR rdata into a string of quoted text strings,
    escaping any embedded double quotes"""
    txtstrings = []
    position = 0
    while position < rdlen:
        slen, = struct.unpack('B', rdata[position:position+1])
        s = rdata[position+1:position+1+slen]
        s = '"{}"'.format(s.replace(b'"', b'"').decode())
        txtstrings.append(s)
        position += 1 + slen
    return ' '.join(txtstrings)
  • I think if you have an extra single quote, i.e. `r'"` should be `r"`. Anyway you can the **decode** function, as in `b'google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs'.decode("utf-8")` as described [here](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – DarrylG Feb 03 '20 at 21:10
  • I use `s = '"{}"'.format(s.replace(b'"', b'"').decode())` to convert to string, but then it does not ignore backslash characters like \010 and take them as escape characters. When I use decode('unicode-escape') the original byte string gets changed aswell at the backslash character. I don't know what to do... – asdfyxcvqwer Feb 03 '20 at 21:24
  • `r'"google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs"` does not seem to be a valid string. – DarrylG Feb 03 '20 at 21:39
  • Did you try just `b'google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs'.decode(("utf-8")`? If so, is the result different from what you want? – DarrylG Feb 03 '20 at 21:41
  • It tells me `'str' object has no attribute 'decode'` and when I use it before converting to a string I get: `google-site-verification=Y3dKKFu_H3cJ3l66OBt0QIUL3 Z6cXR0UxhulSdc28lY` still with line break.. – asdfyxcvqwer Feb 03 '20 at 22:08
  • @asdfyxcvqwer--placed the code in an answer to ensure we are testing with the same code. – DarrylG Feb 03 '20 at 22:36

1 Answers1

0

Use from first answer of

Convert Bytes to String

Assumption

Desired is:

r"google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs"

NOT (which is not valid):

r'"google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs"

# Byte String
s = b'google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs'

# String
decode_s = s.decode('utf-8')

print(f"Byte string\n\t{s}\n\n")
print(f"Decoded Byte string\n\t{decode_s}\n")
print(f'Is decoded == Desired \n{decode_s==r"google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs"}' )

Output

Byte string    
    b'google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs'

Decoded Byte string    
    google-site-verification=pFgmIQ6qK3YjcRAAhsKiPzmEiOVcynQslFMEba5lXvs

Is decoded == Desired
    True
DarrylG
  • 16,732
  • 2
  • 17
  • 23