2

So, if I have

binaryString = "01010101010101001"

How do I convert the string into the binary 01010101010101001

All the pages with similar titled questions only discussed converting strings such as "Hello" into the binary form, not literal zeros and ones.

Edit:

I want to write "010101010101" to a file, but not as the binary value for the 0 and 1 characters.

EDIT:

I want to write "0010101011010" to a file as binary, please could you guide me on this.

I need to convert from "0010101011010" to base 2 number and then string? I am not sure what I would need to write for this.

frazerbw
  • 41
  • 1
  • 6
  • Maybe this answer in a related post could help you: http://stackoverflow.com/questions/18815820/convert-string-to-binary-in-python – juanmajmjr Jul 21 '15 at 12:42
  • What do you perceive as the difference between *"a string of zeros and ones"* and *"binary"*? You can convert it to **an integer**; `int(binaryString, 2)`. – jonrsharpe Jul 21 '15 at 12:43
  • 1
    I'm not sure what you expected as output, but there is no type in Python that is *only binary*. You can convert the string to an integer number represented by the binary notation. – Martijn Pieters Jul 21 '15 at 12:44
  • @juanmajmjr "All the pages with similar titled questions only discussed converting strings such as "Hello" into the binary form, not literal zeros and ones." .. I mentioned this, and you linked to something I have read before. – frazerbw Jul 21 '15 at 12:44
  • @frazerbw I understand what you want, I have found this module that may help. https://docs.python.org/2/library/binascii.html – juanmajmjr Jul 21 '15 at 12:49
  • @juanmajmjr how do I use it? I tried binascii.a2b_qp("101010101") which did not give the correct binary when I printed the binary of the output file to my terminal. – frazerbw Jul 21 '15 at 12:56
  • I will try it and I will let you know! @frazerbw – juanmajmjr Jul 21 '15 at 12:57
  • Have you tried this: `binascii.a2b_uu("01010101010101001")` ? What is your output with that? – juanmajmjr Jul 21 '15 at 13:05
  • 1
    @juanmajmjr using f.write(binascii.a2b_uu("010101010101") I get a very strange and obviously not binary form. The string equivalent of the binary is written as E.PE.PE.PE.PD to give you an idea of the oddness. – frazerbw Jul 21 '15 at 13:10
  • Um... really weird, in the documentation it is written that it will output the binary value. – juanmajmjr Jul 21 '15 at 13:11

0 Answers0