Hi...
I would like to take a string like 'supercalifragilisticexpialidocius'
and write it to a file in binary forms -- this way a user cannot read
the string in case they were try to open in something like ascii text
editor. I'd also like to be able to read the binary formed data back
into string format so that it shows the original value. Is there any
way to do this in Python?
Thanks!
Harlin
import gmpy # GNU Multi-Prceision library for Python
f = open('getty.txt','r')
s = f.read()
f.close
print
print 'source file:'
print
print s
count = 0
f = open('getty_binary.txt','w')
for c in s:
o = ord(c)
b = gmpy.digits(o,2)
d = '0'*(8-len(b)) + b
w = '%s ' % d
f.write(w)
count += 1
if count % 5==0:
f.write('\n')
f.close
f = open('getty_binary.txt','r')
s = f.readlines()
f.close
print
print 'binary file:'
print
for i in s:
print i,
print
c = []
for k in s:
q = k.split()
for m in q:
c.append(chr(int(m,2)))
new_s = ''.join(c)
print
print 'decoded binary:'
print
print new_s
print
## output
## source file:
##
## Four score and seven years ago,
## our fathers brought forth on this continent
## a new nation, conceived in liberty
## and dedicated to the propostion that
## all men are created equal.
##
##
## binary file:
##
## 01000110 01101111 01110101 01110010 00100000
## 01110011 01100011 01101111 01110010 01100101
## 00100000 01100001 01101110 01100100 00100000
## 01110011 01100101 01110110 01100101 01101110
## 00100000 01111001 01100101 01100001 01110010
## 01110011 00100000 01100001 01100111 01101111
## 00101100 00001010 01101111 01110101 01110010
## 00100000 01100110 01100001 01110100 01101000
## 01100101 01110010 01110011 00100000 01100010
## 01110010 01101111 01110101 01100111 01101000
## 01110100 00100000 01100110 01101111 01110010
## 01110100 01101000 00100000 01101111 01101110
## 00100000 01110100 01101000 01101001 01110011
## 00100000 01100011 01101111 01101110 01110100
## 01101001 01101110 01100101 01101110 01110100
## 00001010 01100001 00100000 01101110 01100101
## 01110111 00100000 01101110 01100001 01110100
## 01101001 01101111 01101110 00101100 00100000
## 01100011 01101111 01101110 01100011 01100101
## 01101001 01110110 01100101 01100100 00100000
## 01101001 01101110 00100000 01101100 01101001
## 01100010 01100101 01110010 01110100 01111001
## 00001010 01100001 01101110 01100100 00100000
## 01100100 01100101 01100100 01101001 01100011
## 01100001 01110100 01100101 01100100 00100000
## 01110100 01101111 00100000 01110100 01101000
## 01100101 00100000 01110000 01110010 01101111
## 01110000 01101111 01110011 01110100 01101001
## 01101111 01101110 00100000 01110100 01101000
## 01100001 01110100 00001010 01100001 01101100
## 01101100 00100000 01101101 01100101 01101110
## 00100000 01100001 01110010 01100101 00100000
## 01100011 01110010 01100101 01100001 01110100
## 01100101 01100100 00100000 01100101 01110001
## 01110101 01100001 01101100 00101110 00001010
##
##
## decoded binary:
##
## Four score and seven years ago,
## our fathers brought forth on this continent
## a new nation, conceived in liberty
## and dedicated to the propostion that
## all men are created equal.