In [3]:
# Program by Rakesh Kumar, rakesh4osdd@gmail.com completed on 31 July 2017
# convert two line binary data into binary file, import modules
from cStringIO import StringIO
import binascii

In [6]:
def int2bin(fpbits):
    sio = StringIO(fpbits)
    outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    f = open(outfile, 'w+b')
    fpbytes = ''
    # when condition is true (1)
    while 1:
        # get the next 8 bits
        b = sio.read(8)
        # CHECK THE READING STRING
        #print ("stop")
        #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)
        fpbytes = c + fpbytes
        # Write the file
        f.write(c)
    f.close()

    #read file as binary data to check the input and output
    #outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    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 (fpbits)
    # check string by matching
    # find padding and create original string by removal of padding
    print len(fpout),len(fpbits)
    padding = len(fpout) - len(fpbits)
    print padding
    if (padding == 0):
        fpout1 = fpout
    else:
        fpout1 = fpout[:-padding]
    print ("bit with padding", fpout1)
    # actual string
    print ("actual" , fpbits)
    print ("Is input and output string bits are equal?", "Ans:", (fpbits == fpout1))
    #return f
    return fpbytes

In [9]:
print ('Value', int2bin('0101010'))
#this function works


01010100
54
01010100
01010100
0101010
8 7
1
('bit with padding', '0101010')
('actual', '0101010')
('Is input and output string bits are equal?', 'Ans:', True)
('Value', 'T')

In [ ]:


In [14]:
# this function word not correctly as per requirements need to crosscheck
import os
ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna2word.bin"
# open file in read write mode of binary
fout = open('bigbin.bin','w+b')
renamefile = ""
fpbits=[]
with open(ifile,"r") as f:
    for l in f:
        fpbits.append(l)
        l=map(str.rstrip,l)
        m = ''.join(l)
        #print len(m)
        print ("value M" , m)
        fpfile = int2bin(m)
        print  ("value F", fpfile)
        #fout = fpfile.read()
        #print("test file", fpfile, fpfile.name)
        #fpfile1 = open (fpfile.name, 'rb')
        #print("test file", fpfile1)
        fout.write(fpfile)
        print (fout.read())
    fout.close()
#os.rename(fout, newfile )
os.system('mv bigbin.bin dna2word.bin.txt')


('value M', '011001000110111001100001001011010011001000101101')
646e612d322d
011001000110111001100001001011010011001000101101
011001000110111001100001001011010011001000101101
011001000110111001100001001011010011001000101101
48 48
0
('bit with padding', '011001000110111001100001001011010011001000101101')
('actual', '011001000110111001100001001011010011001000101101')
('Is input and output string bits are equal?', 'Ans:', True)
('value F', '-2-and')

('value M', '011101110110111101110010011001000000101001100100')
776f72640a64
011101110110111101110010011001000000101001100100
011101110110111101110010011001000000101001100100
011101110110111101110010011001000000101001100100
48 48
0
('bit with padding', '011101110110111101110010011001000000101001100100')
('actual', '011101110110111101110010011001000000101001100100')
('Is input and output string bits are equal?', 'Ans:', True)
('value F', 'd\ndrow')

('value M', '011011100110000100101101001100100010110101100010')
6e612d322d62
011011100110000100101101001100100010110101100010
011011100110000100101101001100100010110101100010
011011100110000100101101001100100010110101100010
48 48
0
('bit with padding', '011011100110000100101101001100100010110101100010')
('actual', '011011100110000100101101001100100010110101100010')
('Is input and output string bits are equal?', 'Ans:', True)
('value F', 'b-2-an')

('value M', '011010010110111000001010')
696e0a
011010010110111000001010
011010010110111000001010
011010010110111000001010
24 24
0
('bit with padding', '011010010110111000001010')
('actual', '011010010110111000001010')
('Is input and output string bits are equal?', 'Ans:', True)
('value F', '\nni')

Out[14]:
0

In [ ]:


In [ ]: