In [198]:
# convert binary integer string to binary data string
from cStringIO import StringIO
import binascii
#fp = "010101010101010101010101010101010101010101010101"
#fp = "10011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110"
# for a given 0,1 integer in a file
#ifile="/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna1.bin"
ifile="/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna1_6"
fpbits=[]
with open(ifile,"r") as f:
for l in f:
fpbits.append(l)
#print ("first loop", l)
# strip newline character from fingerprint bits string
fpbits=map(str.rstrip,fpbits)
fp = ''.join(fpbits)
#fp = "010101010101010101010101010101010101010101010101"
print len(fp)
sio = StringIO(fp)
outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
In [199]:
f = open(outfile, 'wb')
In [200]:
# when condition is true (1)
while 1:
# get the next 8 bits
b = sio.read(8)
# CHECK THE READING STRING
print (b)
# check if hit EOF
if not b:
break
# If less than 8 bits, pad with zeroes (0's) on the right
if len(b) < 8:
b = b + '0' * (8 - len(b))
print (b)
# Convert to integer (int)
i = int(b, 2)
#print (i)
# Convert to character (chr)
c = chr(i)
#print (c)
# Write the file
f.write(c)
In [201]:
f.close()
In [202]:
# read file as binary data to check the input and output
binFile = open(outfile,'rb')
binaryData = binFile.read(32)
hexvalue = binascii.hexlify(binaryData)
print hexvalue
binary = binascii.a2b_hex(hexvalue)
# print output file having padding bit and print input string
print "".join("{:08b}".format(ord(i)) for i in binary)
fpout = "".join("{:08b}".format(ord(i)) for i in binary)
print (fpout)
print (fp)
In [203]:
# check string by matching
# find padding and create original string by removal of padding
print len(fpout),len(fp)
padding = len(fpout) - len(fp)
print padding
if (padding == 0):
fpout1 = fpout
else:
fpout1 = fpout[:-padding]
print (fpout1)
# actual string
print (fp)
print ("Is input and output string bits are equal?", "Ans:", (fp == fpout1))
In [ ]:
In [ ]:
In [ ]:
In [ ]: