Python从十六进制转换为二进制字符串并返回十六进制

时间:2014-11-26 22:18:56

标签: python io hex

我正在尝试制作一个脚本,它会反转文件中的每一位然后保存它。 XOR基本上就是。

###########OUTPUT############################

53596
0b
53595.875
0.12500029153363015
['1', '0', '0', '1', '1', '0', '1', '0']
Len of a before reverse: 428767
['0', '1', '1', '0', '0', '1', '0', '1']
Len of a after reverse, as you can notice it's ODD: 428767
Number of bytes = 107192
Number of bytes * 4 = 428768
32a
b'2\xa5'
b'2\xa5o\xff'
TEST
255
255
255
0b10000000
80
###########CODE############################
import binascii
import time


def change(s, i): #Fucntion for changing 0s to 1s, and 1s to 0s.
    if s == "1":
        a[i] = "0"
        return

    if s == "0":
        a[i] = "1"
        return


b = open("C:/Users/pol/Desktop/test.exe", "rb") #Open up file.
a = b.read(-1) #Read its data, which is given to us in hex.
b.close()
b = open("C:/Users/pol/Desktop/testCloneReversed.exe", "wb") #Open up another file where we'll be writing 

p = len(a)
print(p)

print(bin(int(binascii.hexlify(a), 16))[:2]) #Just to check what we're cutting off.
a = bin(int(binascii.hexlify(a), 16))[2:]#From hex to a raw binary string.  
a = list(a)

print(len(a) / 8)
print(p / len(a)) #1 Bit is gone?

print(a[:8]) #['1', '0', '0', '1', '1', '0', '1', '0']
print("Len of a before reverse: " + str(len(a)))

i = 0
for s in a: #Change all 0s to 1s, and all 1s to 0s.
    change(s, i)
    i = i + 1

print(a[:8]) #['0', '1', '1', '0', '0', '1', '0', '1']This works nicely.
print("Len of a after reverse, as you can notice it's ODD: " + str(len(a)))

a = ''.join(a) #From list to str.

a = hex(int(a, 2))[2:] #From raw binary to hex.
print("Number of bytes = " + str(len(a)))
print("Number of bytes * 4 = " + str(len(a) * 4)) #We gain a bit.


print(a[:3]) #0x32, which is 00110010, as you can see it's wrong.
a = binascii.unhexlify(a) #From hexlified to unhexlfied.
b.write(a)

type(a) #Bytes
print(a[:2]) #b'2\xa5'
#2a = 00101010
#2a5 = 001010100101
#a5 = 10100101
print(a[:4]) #b'2\xa5o\xff' Not sure how to read this.

b.close()

####TEST
print("TEST")
m = 0xFF
print(m)
print(str(m))
m = (int(str(m), 10))
print(m)


a = bin(128) #0x80 in hex.
print(a) #0b10000000, or 0x80 in hex or 128 in decimal.
a = hex(int(a, 2))[2:]
print(a) #Prints 0x80, which is 10000000 in binary and is right.

我不是最擅长的数学家,但我认为在将十六进制转换为原始二进制字符串时我们会失去一点。

然后,我相信二进制位移了1位或类似的东西。是的,我不完全确定发生了什么,有人有想法吗?

1 个答案:

答案 0 :(得分:0)

看起来您可能会丢失字节字符串中的前导零。以下是替换更改功能的示例。你不应该在方法或函数中修改全局变量(当然,这是一个例子,但是)..

#!python
from bitstring import BitArray

def change(byte):
    """ create bit array from a hex string that was
        derived from the ordinal value of the ascii
        character
    """
    changeling = BitArray("{0:#x}".format(ord(byte)))
    # invert the bits
    changeling.invert()
    # get the new integer value of the byte
    int_value = changeling.uint
    # convert it back to ascii and return it
    return chr(int_value)

x='a'
x_ord=ord(x)

print("char: %s" % (x))
print("bin: {0:#010b}".format(x_ord))

x = change(x)
x_ord=ord(x)

print("char: %s" % (x))
print("bin: {0:#010b}".format(x_ord))

Output:
char: a
bin: 0b01100001
char: ▒
bin: 0b10011110