Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从十六进制转换为二进制字符串并返回十六进制_Python_Io_Hex - Fatal编程技术网

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

Python从十六进制转换为二进制字符串并返回十六进制,python,io,hex,Python,Io,Hex,我正在尝试制作一个脚本,它反转文件中的每一位,然后保存它。基本上是异或 ###########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

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

###########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位或类似的东西。是的,我不完全确定发生了什么,有人有主意吗?

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

#!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