将原始字符串转换回十六进制字符

Converting a Raw String back to Hex Characters

我需要将一系列十六进制字符转换为原始字符串以便对其应用修改:

chars = r"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"

此修改要求用户输入(未包含在代码中)并从字符串中删除一个或多个字符:

def charsMod(charsResultInput):
    global chars
    toRemove = charsResultInput.split(' ')
    toRemove = list(r'\x' + toRemove for toRemove in toRemove)
    for match in toRemove:
        chars = chars.replace(match, '')
    return chars

例如,如果用户输入“01 02”,则上述函数returns的输出为“\x03\x04[...]”,省略了“\x01”和“\ x02

但是,我现在需要一种方法将此字符串(仍然是原始字符串)转换回普通字符串(这样它就不是 r[x]"),这样输出就不再是读取为原始字符串。

因此我需要输出看起来类似于:


123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

而不是:

\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f[...]

所以我需要以下方面的帮助:

感谢您的帮助。我是 Python.

的新手

您也可以使用字节数组代替原始字符串。

chars = bytearray(b'0x00x10x20x30x40x50x60x70x80x90xa0xb0xc0xd0xe0xf0x100x110x120x130x140x150x160x170x180x190x1a0x1b0x1c0x1d0x1e0x1f0x200x210x220x230x240x250x260x270x280x290x2a0x2b0x2c0x2d0x2e0x2f0x300x310x320x330x340x350x360x370x380x390x3a0x3b0x3c0x3d0x3e0x3f0x400x410x420x430x440x450x460x470x480x490x4a0x4b0x4c0x4d0x4e0x4f0x500x510x520x530x540x550x560x570x580x590x5a0x5b0x5c0x5d0x5e0x5f0x600x610x620x630x640x650x660x670x680x690x6a0x6b0x6c0x6d0x6e0x6f0x700x710x720x730x740x750x760x770x780x790x7a0x7b0x7c0x7d0x7e0x7f0x800x810x820x830x840x850x860x870x880x890x8a0x8b0x8c0x8d0x8e0x8f0x900x910x920x930x940x950x960x970x980x990x9a0x9b0x9c0x9d0x9e0x9f0xa00xa10xa20xa30xa40xa50xa60xa70xa80xa90xaa0xab0xac0xad0xae0xaf0xb00xb10xb20xb30xb40xb50xb60xb70xb80xb90xba0xbb0xbc0xbd0xbe0xbf0xc00xc10xc20xc30xc40xc50xc60xc70xc80xc90xca0xcb0xcc0xcd0xce0xcf0xd00xd10xd20xd30xd40xd50xd60xd70xd80xd90xda0xdb0xdc0xdd0xde0xdf0xe00xe10xe20xe30xe40xe50xe60xe70xe80xe90xea0xeb0xec0xed0xee0xef0xf00xf10xf20xf30xf40xf50xf60xf70xf80xf90xfa0xfb0xfc0xfd0xfe0xff')

然后您可以进行替换,完成后使用 chr() 将十六进制转换回对应的字符串。

chars = r"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"

def charsMod(charsResultInput):
    chars_chars_mod = chars
    
    # "01 02"
    inp = charsResultInput.split()
    for i in inp:
        # get rid of each value the user gave us
        chars_chars_mod = chars_chars_mod.replace(f"{i}\x","")
    
    # get the hex values by themselves ( eg. [03, 04, 05, ...] )
    arr = chars_chars_mod.split("\x")
    
    arr = "".join( # base 16 for hex
        chr(int(i,16)) # turn each hex into an int, then convert
                        # that int into its ascii value 
        for i in arr if i # "if i" to get rid of blank matches
        )
    return arr
    

print(
    charsMod("01 02")
    )

# Outputs -->
"""
    


 
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûþÿ
"""