In [7]:
# convert two line binary data into binary file, import modules
from cStringIO import StringIO
import binascii

In [8]:
def int2bin(fpbits):
    sio = StringIO(fpbits)
    outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    f = open(outfile, 'wb')
    # 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)

        # 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

In [14]:
ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/example-words-bin.txt"
# open file in read write mode of binary
fout = open('bigbin.bin','r+b')
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)
        fpfile = int2bin(m)
        #fout = fpfile.read()
        #print("test file", fpfile, fpfile.name)
        fpfile1 = open (fpfile.name, 'rb')
        #print("test file", fpfile1)
        fout.write(fpfile1.read())
        print (fout.read())
    fout.close()


5468697320746865206578616d706c652066696c6520666f7220627974652074
0101010001101000011010010111001100100000011101000110100001100101001000000110010101111000011000010110110101110000011011000110010100100000011001100110100101101100011001010010000001100110011011110111001000100000011000100111100101110100011001010010000001110100
0101010001101000011010010111001100100000011101000110100001100101001000000110010101111000011000010110110101110000011011000110010100100000011001100110100101101100011001010010000001100110011011110111001000100000011000100111100101110100011001010010000001110100
0101010001101000011010010111001100100000011101000110100001100101001000000110010101111000011000010110110101110000011011000110010100100000011001100110100101101100011001010010000001100110011011110111001000100000011000100111100101110100011001010010000001110100011011110010000001100010011010010111010000100000011100000111001001101111011001110111001001100001011011010010111000001010
256 376
-120
('bit with padding', '010101000110100001101001011100110010000001110100011010000110010100100000011001010111100001100001011011010111000001101100')
('actual', '0101010001101000011010010111001100100000011101000110100001100101001000000110010101111000011000010110110101110000011011000110010100100000011001100110100101101100011001010010000001100110011011110111001000100000011000100111100101110100011001010010000001110100011011110010000001100010011010010111010000100000011100000111001001101111011001110111001001100001011011010010111000001010')
('Is input and output string bits are equal?', 'Ans:', False)

48657265206973206d756c7469706c65206c696e6573206173206578616d706c
0100100001100101011100100110010100100000011010010111001100100000011011010111010101101100011101000110100101110000011011000110010100100000011011000110100101101110011001010111001100100000011000010111001100100000011001010111100001100001011011010111000001101100
0100100001100101011100100110010100100000011010010111001100100000011011010111010101101100011101000110100101110000011011000110010100100000011011000110100101101110011001010111001100100000011000010111001100100000011001010111100001100001011011010111000001101100
0100100001100101011100100110010100100000011010010111001100100000011011010111010101101100011101000110100101110000011011000110010100100000011011000110100101101110011001010111001100100000011000010111001100100000011001010111100001100001011011010111000001101100011001010010000001101111011001100010000001100010011010010110111001100001011100100111100100100000011010010110111001100110011011110111001001101101011000010111010001101001011011110110111000100000011010010110111000100000011000010010000001000011010100110101011000100000011001100110100101101100011001010010111000001010
256 568
-312
('bit with padding', '0100100001100101011100100110010100100000011010010111001100100000011011010111010101101100011101000110100101110000011011000110010100100000011011000110100101101110011001010111001100100000011000010111001100100000011001010111100001100001011011010111000001101100')
('actual', '0100100001100101011100100110010100100000011010010111001100100000011011010111010101101100011101000110100101110000011011000110010100100000011011000110100101101110011001010111001100100000011000010111001100100000011001010111100001100001011011010111000001101100011001010010000001101111011001100010000001100010011010010110111001100001011100100111100100100000011010010110111001100110011011110111001001101101011000010111010001101001011011110110111000100000011010010110111000100000011000010010000001000011010100110101011000100000011001100110100101101100011001010010111000001010')
('Is input and output string bits are equal?', 'Ans:', False)

546865206279746520746f2062696e6172792070726f6772616d206973207772
0101010001101000011001010010000001100010011110010111010001100101001000000111010001101111001000000110001001101001011011100110000101110010011110010010000001110000011100100110111101100111011100100110000101101101001000000110100101110011001000000111011101110010
0101010001101000011001010010000001100010011110010111010001100101001000000111010001101111001000000110001001101001011011100110000101110010011110010010000001110000011100100110111101100111011100100110000101101101001000000110100101110011001000000111011101110010
01010100011010000110010100100000011000100111100101110100011001010010000001110100011011110010000001100010011010010110111001100001011100100111100100100000011100000111001001101111011001110111001001100001011011010010000001101001011100110010000001110111011100100110100101110100011001010110111000100000011010010110111000100000010000110101001101001001010100100010110101001001010011010101010001000101010000110100100000001010
256 416
-160
('bit with padding', '0101010001101000011001010010000001100010011110010111010001100101001000000111010001101111001000000110001001101001011011100110000101110010011110010010000001110000')
('actual', '01010100011010000110010100100000011000100111100101110100011001010010000001110100011011110010000001100010011010010110111001100001011100100111100100100000011100000111001001101111011001110111001001100001011011010010000001101001011100110010000001110111011100100110100101110100011001010110111000100000011010010110111000100000010000110101001101001001010100100010110101001001010011010101010001000101010000110100100000001010')
('Is input and output string bits are equal?', 'Ans:', False)

303130303130303020726570726573656e7420746865206c617374206c696e65
0011000000110001001100000011000000110001001100000011000000110000001000000111001001100101011100000111001001100101011100110110010101101110011101000010000001110100011010000110010100100000011011000110000101110011011101000010000001101100011010010110111001100101
0011000000110001001100000011000000110001001100000011000000110000001000000111001001100101011100000111001001100101011100110110010101101110011101000010000001110100011010000110010100100000011011000110000101110011011101000010000001101100011010010110111001100101
001100000011000100110000001100000011000100110000001100000011000000100000011100100110010101110000011100100110010101110011011001010110111001110100001000000111010001101000011001010010000001101100011000010111001101110100001000000110110001101001011011100110010100100000011001010110111001100100001000000110001101101000011000010111001001100001011000110111010001100101011100100010000001001000
256 384
-128
('bit with padding', '00110000001100010011000000110000001100010011000000110000001100000010000001110010011001010111000001110010011001010111001101100101')
('actual', '001100000011000100110000001100000011000100110000001100000011000000100000011100100110010101110000011100100110010101110011011001010110111001110100001000000111010001101000011001010010000001101100011000010111001101110100001000000110110001101001011011100110010100100000011001010110111001100100001000000110001101101000011000010111001001100001011000110111010001100101011100100010000001001000')
('Is input and output string bits are equal?', 'Ans:', False)


In [ ]:


In [ ]: