0

I have this line in my program: TextV = base64.b64decode('0cQ+bNsObaw=') and the value of TextV is b'\xd1\xc4>l\xdb\x0em\xac'. Then I run this to convert TextV to binary:

TextVBin = ''.join(format(x, 'b') for x in bytearray(TextV))

and the value of TextVBin is '11010001110001001111101101100110110111110110110110101100'. Now, I wanna again convert TextVBin format to TextV format(i.e. b'\xd1\xc4>l\xdb\x0em\xac') But I googled and I couldn't find any answer. How can I do this in Python 3?

7
  • Maybe you're looking for this? Commented Oct 31, 2016 at 13:45
  • So the question I linked to solved your problem? If so, I'll mark this a dup target. Commented Oct 31, 2016 at 13:54
  • No, I don't get the output that I want, I get this: b'\xd1\xc4\xfbf\xdfm\xac' that is different with b'\xd1\xc4>l\xdb\x0em\xac' Commented Oct 31, 2016 at 13:55
  • So how did you solve your problem? Commented Oct 31, 2016 at 13:59
  • if you did steps A->B->C then now you have to do C->B->A. first you have to split text, then convert to bytearray, etc. Commented Oct 31, 2016 at 14:04

2 Answers 2

2

I would use:

import struct
TextVBin = "{:b}".format(struct.unpack(">Q", TextV)[0])

to convert your TextV to binary string.

It however produces 1101000111000100001111100110110011011011000011100110110110101100 which is different than your own output, but I guess that is because leading 0 are truncated for each byte with your own method. So your method is wrong.

using struct: 1101000111000100001111100110110011011011000011100110110110101100

using yours: 1101000111000100 111110 110110011011011 1110 110110110101100

Then to convert this binary string back to bytes:

int('1101000111000100001111100110110011011011000011100110110110101100', 2).to_bytes(8, 'big')

Note: I assumed that your TextVBin is 8 bytes long and big endian based on your example. If length is variable, my answer does not apply.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I use struct.unpack for 16 bytes object? It seems this is just for 8 bytes object
0

The problem is that your format string truncates leading zeros. You should use

TextVBin = ''.join('{:08b}'.format(x) for x in bytearray(TextV))

which will format each byte with exactly 8 binary digits. Then, to reverse, simply do

TextV = bytearray([int(TextVBin[i:i+8], 2) for i in range(0, len(TextVBin), 8)])

For example:

>>> import base64
>>> TextV = base64.b64decode('0cQ+bNsObaw=')
>>> TextV
b'\xd1\xc4>l\xdb\x0em\xac'
>>> TextVBin = ''.join('{:08b}'.format(x) for x in bytearray(TextV))
>>> TextVBin
'1101000111000100001111100110110011011011000011100110110110101100'
>>> TextV = bytearray([int(TextVBin[i:i+8], 2) for i in range(0, len(TextVBin), 8)])
>>> TextV
bytearray(b'\xd1\xc4>l\xdb\x0em\xac')

2 Comments

How can I remove bytearray from this?i.e, just return b'\xd1\xc4>l\xdb\x0em\xac'
On Python 3, use the bytes constructor instead of bytearray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.