Python修改二进制文件

时间:2017-05-13 21:12:41

标签: python binary-data

好的,没有太多的杂草。我试图获取一个二进制文件并一次读取一个字节,将其转换为4位并将其与我读取的下一个字节合并,并转换为4位以重新创建一个新字节。

我这样做是通过读取一个字节值来将它与一个查找表进行比较,该查找表获取最小值和最大值,并为我的“插槽”除以16。然后逐个查看值并根据这些插槽存储新值(0-15二进制4位),移位该数字,然后在我读取的第一个字节读取时读入下一个字节的XOR。 / p>

我的程序似乎正在工作并写入1和0但我认为它将它们存储为字符串而不是二进制文件。有什么想法吗?

import sys
import binascii

def converttwobytes(value1, value2):
    newvalue = 0
    newvalue = newvalue ^ value1
    newvalue1 = newvalue << 4
    newvalue = newvalue1 ^ value2
    return newvalue


if (len(sys.argv) != 3):
    print ('\n\n\nPlease enter 3 parameters: the file name to run, a file to read, a file to write to and try again, example: python prgm1.py read.txt write.txt\n\n\n')
else:
    readfile = sys.argv[1]
    writefile = sys.argv[2]
    with open(readfile, "rb") as binary_file:
    # Read the whole file at once
    data = binary_file.read()
    length = len(data)

    framelength = 16


    # Seek position set to 0 and read header of 21 bytes
    binary_file.seek(0)  # Go to beginning
    header = binary_file.read(21)
    counter = 21
    with open(writefile, 'wb') as writeline:
        writeline.write(header + '\n')

        for filesize in range(counter,length,16):
            #latest seek value
            binary_file.seek(filesize)
            maxnum = 0
            minnum = 0
            for frame in range(0,framelength):
                value = ord(binary_file.read(1))
                if frame == 0:
                    minnum = value
                    maxnum = minnum
                elif value > maxnum:
                    maxnum = value
                elif value < minnum:
                    minnum = value


            totalspread = maxnum - minnum
            binary_file.seek(filesize)
            # 16 is max number of 4 bits binary
            bucketsize = (totalspread/16)

            #reset seek back to frame start location
            binary_file.seek(filesize)
            newlist = []
            for frame in range(0,framelength):
                value = ord(binary_file.read(1))
                result =0
                if value < (minnum+bucketsize):
                    result = 0
                elif value < (minnum+(bucketsize*2)):
                    result =1
                elif value < (minnum+(bucketsize*3)):
                    result =2
                elif value < (minnum+(bucketsize*4)):
                    result =3
                elif value < (minnum+(bucketsize*5)):
                    result =4
                elif value < (minnum+(bucketsize*6)):
                    result =5
                elif value < (minnum+(bucketsize*7)):
                    result =6
                elif value < (minnum+(bucketsize*8)):
                    result =7
                elif value < (minnum+(bucketsize*9)):
                    result =8
                elif value < (minnum+(bucketsize*10)):
                    result =9
                elif value < (minnum+(bucketsize*11)):
                    result =10
                elif value < (minnum+(bucketsize*12)):
                    result =11
                elif value < (minnum+(bucketsize*13)):
                    result =12
                elif value < (minnum+(bucketsize*14)):
                    result =13
                elif value < (minnum+(bucketsize*15)):
                    result =14
                elif value < (minnum+(bucketsize*16)):
                    result =15

                newlist.append(result)
            newlist.reverse()
            i = 0
            while i < len(newlist):
                firstfour = newlist.pop()
                lastfour = newlist.pop()

                newbyte = converttwobytes(firstfour, lastfour)

                writeline.write(bin(newbyte)[2:].zfill(8))
                i = i + 2

这是我尝试编码和解码的文件示例:

 P5

 256 256

 255




ææææåææäèééçæèèèæçèêæèééèéëêêçëêêêéêëéëêëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëëéëëéèèåæåäåäæéàãäáãäáãàââããäááâãâäâäääãääääæäçëéêéèéééææèèåçèäåååèãæääåèæçæçèåçæçæåææãâáàäææäâßäääæäáããâááâãÞàáæâÞáÞßàÚÛÜÜÝßÞÝßÛßàÙÝßßÛÜÛÛÝÜÚÛÚ×ÕÚÙÖÚÖÓÖÔÓÔÕÒÕÒÔÔÓÓÒÒÏÏÐÍÌÍÑËÉÉÆÊÍÇçëéçéçéäãèçèççêèææèçæåæåèèèæææçæèçèêéççêéèèæéêëéêêëëëëéëëéëêééëëëëëëëëëëëëëëëëëéëèêèææèêéééäåååãäååääåæäãããäãâååããæçåçççèèèçææèèæççéçèæåææèèæææãåæçåæåçæèæçèåçèéçäãåãääßääåäåãäãåããäãåâáÞáßßÞâßßÜÞ

1 个答案:

答案 0 :(得分:0)

您将重新接收的输入将是一个字符串,因此您需要对其进行转换:

bin(int(binascii.hexlify(str(result)), 16))