0
''.join(format(ord(i), 'b') for i in string)

This line of code gives the binary representation of string data, now for me to edit the binary version I have to convert the binary data representation to string again. Like this:

string = 'hello'
bin_string = ''.join(format(ord(i), 'b') for i in string)
print (bin_string)
# Output: 11010001100101110110011011001101111

bin_string = str(bin_string)

#even if I don't convert it to string it will end up a string because I edited it

Now let's say bin_string ended up as '11010001100101101010101001101111' after my edits, is there a way of getting it back to a binary datatype and not save the 0s and 1s as a string?

4
  • 1
    bin_string = str(bin_string) does nothing, because bin_string was already an instance of str. Commented Nov 11, 2020 at 21:02
  • 3
    Purely out of curiosity, what is the use case? Commented Nov 11, 2020 at 21:03
  • 1
    Yes, you can convert a string to binary, but why do you even convert it to string in the first place? This is way more complex to implement, produces code difficult to understand and is terribly slow. Just edit the binary data directly. Commented Nov 11, 2020 at 21:07
  • 1
    Depends on if '11010001100101101010101001101111' is a string of consistent (n) size bit lengths. Commented Nov 11, 2020 at 21:36

3 Answers 3

3

No, because the different strings are converted to the same bit pattern by your transformation.

In [6]: ''.join(format(ord(i), 'b') for i in '\1\0')                            
Out[6]: '10'

In [7]: ''.join(format(ord(i), 'b') for i in '\2')                              
Out[7]: '10'
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, so it is even worse than expected, because the conversion to string is not a good one.
Interesting example. Learned something new, thanks!
1

All you have to do is append 0b to the front of the string and it is saved as a binary literal.

>>> binary = 0b01001010
>>> bin(binary)
'0b1001010'
>>> binary
74

So, if you append 0b to the front of a string of binary it will be saved as a binary literal which I think is what you are asking for?

Check this post out.

2 Comments

Surely you meant to use 73 ... a binary palindrome. ;-)
Thank you so much, this helped me a lot and pretty sure solved my problem. I really really appreciate your help.
1

You'd have to know where to split the binary string to convert it back to integers, which you'd then convert to characters using chr. For example, if you did

bin_string = '|'.join(format(ord(i), 'b') for i in string)

you could then reverse it by:

mod_string = ''.join(chr(int(x, 2)) for x in bin_string.split('|'))

Alternatively, if you ensured each binary representation had the same length (e.g. 8 digits), you could convert the string back in chunks of characters.

# First keep all characters separate in a list
bin_list = [format(ord(i), 'b') for i in string]

# Find the max length for the chunk size
chunk_size = max(len(x) for x in bin_list)

# zfill fills to chunk_size with zeros
bin_string = ''.join(x.zfill(chunk_size) for x in bin_list)

# Shenanigans to modify bin_string

# split bin_string into chunks
chunks = [bin_string[i:i+chunk_size] for i in range(0, len(bin_string), chunk_size)]

# Convert chunks back to characters
mod_string = ''.join(chr(int(x, 2)) for x in chunks)

1 Comment

Thank you so much, I will definitely try using this method. I really appreciate your help.

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.