I want to take a string of 1's and 0's and convert it into an actual binary file(simply writing the string of 1's and 0's to a file would just make it either ascii file containing "00110001"s and "00110000"s ). I would prefer to do this in python or directly from a bash shell, but java or C is fine too. this is probably a one time use.
Thanks.
Binary is a base-2 number system invented by Gottfried Leibniz that's made up of only two numbers or digits: 0 (zero) and 1 (one). This numbering system is the basis for all binary code, which is used to write digital data such as the computer processor instructions used every day.
In Python, use the int
built-in function to convert the string of 0s and 1s to a number:
>>> int("00100101", 2)
37
Then use the chr
built-in to convert a 8-bit integer (that is, in the inclusive range 0-255) to a character.
>>> chr(_)
'%'
The result of chr
can be simply written to a file (opened in binary mode) with the file.write
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With