In [211]:
# 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_156"
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'


156

In [212]:
f = open(outfile, 'wb')

In [213]:
# 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)


01011011
01110010
11011011
10010110
11011100
10110110
11100101
10110111
00101101
10111001
01101101
11001011
01101110
01011011
01110010
11011011
10010110
11011100
10110110
1110
11100000


In [214]:
f.close()

In [217]:
# 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)


5b72db96dcb6e5b72db96dcb6e5b72db96dcb6e0
0101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100000
0101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100000
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110

In [218]:
# 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))


160 156
4
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110
('Is input and output string bits are equal?', 'Ans:', True)