samo
samo

Reputation: 43

Get binary data from string

After sending some information through a socket i have a binary object that looks like this:

b"1:b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

What I want to do is get the first part before the : as a string and the second as a binary. Like this:

'1'

and:

b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'

With all my attempts I ended up with either:

b"b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

or:

"b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

Upvotes: 1

Views: 87

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160467

Just split it on b':' and decode and trim accordingly:

i, j = r.split(b':')
i = i.decode()  #  '1'
j = j[2:-1]

Now:

print(i)
# 1
print(j)
# b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'

Upvotes: 1

Related Questions