使用String中的HEX字符追加String

问题描述 投票:1回答:2

我还没有找到解决这个问题的方法:

从用户输入脚本接收一个字符串,例如:

“ABCD.00.00”

的。表示十六进制字符,脚本应输出以下字符串:

“ABCD \ X00 \ X00”

我尝试了以下方法:

cmd = "ABCD.00.00"
if "." in cmd:
    splitted = cmd.split(".")
    cmd = splitted[0]
    i = 1
    while i < len(splitted):
        cmd = cmd+"\\x"+splitted[i]
        i = i + 1
return(cmd)

但返回的字符串有两个反斜杠,因此无法识别十六进制字符:

“ABCD \\ \\ X00 X00”

有没有人有解决这个问题的线索?提前谢谢了

python string hex character
2个回答
0
投票

这是因为根据文档,unicode字符串不能直接附加到字符串(utf-8)。

编码不必处理每个可能的Unicode字符,并且大多数编码不会。例如,将Unicode字符串转换为ASCII编码的规则很简单;对于每个代码点:

如果代码点<128,则每个字节与代码点的值相同。如果代码点为128或更大,则无法在此编码中表示Unicode字符串。 (在这种情况下,Python会引发UnicodeEncodeError异常。)

如果您将打印该功能的输出,它将为您提供所需的输出

>>> a="ABCD\\x00\\x00"
>>> print(a)
ABCD\x00\x00

0
投票

我只是通过使用查找表尝试了一种快速而又脏的方法:

ascii2hex = (['00', '\x00'], ['01', '\x01'], ['02', '\x02'], ['03', '\x03'], ['04', '\x04'],
        ['05', '\x05'], ['06', '\x06'], ['07', '\x07'], ['08', '\x08'], ['09', '\x09'],
        ['0A', '\x0A'], ['0B', '\x0B'], ['0C', '\x0C'], ['0D', '\x0D'], ['0E', '\x0E'], ['0F', '\x0F'],
        ['10', '\x10'], ['11', '\x11'], ['12', '\x12'], ['13', '\x13'], ['14', '\x14'],
        ['15', '\x15'], ['16', '\x16'], ['17', '\x17'], ['18', '\x18'], ['19', '\x19'],
        ['1A', '\x1A'], ['1B', '\x1B'], ['1C', '\x1C'], ['1D', '\x1D'], ['1E', '\x1E'], ['1F', '\x1F'],
        ['20', '\x20'], ['21', '\x21'], ['22', '\x22'], ['23', '\x23'], ['24', '\x24'],
        ['25', '\x25'], ['26', '\x26'], ['27', '\x27'], ['28', '\x28'], ['29', '\x29'],
        ['2A', '\x2A'], ['2B', '\x2B'], ['2C', '\x2C'], ['2D', '\x2D'], ['2E', '\x2E'], ['2F', '\x2F'],
        ['30', '\x30'], ['31', '\x31'], ['32', '\x32'], ['33', '\x33'], ['34', '\x34'],
        ['35', '\x35'], ['36', '\x36'], ['37', '\x37'], ['38', '\x38'], ['39', '\x39'],
        ['3A', '\x3A'], ['3B', '\x3B'], ['3C', '\x3C'], ['3D', '\x3D'], ['3E', '\x3E'], ['3F', '\x3F'],
        ['40', '\x40'], ['41', '\x41'], ['42', '\x42'], ['43', '\x43'], ['44', '\x44'],
        ['45', '\x45'], ['46', '\x46'], ['47', '\x47'], ['48', '\x48'], ['49', '\x49'],
        ['4A', '\x4A'], ['4B', '\x4B'], ['4C', '\x4C'], ['4D', '\x4D'], ['4E', '\x4E'], ['4F', '\x4F'],
        ['50', '\x50'], ['51', '\x51'], ['52', '\x52'], ['53', '\x53'], ['54', '\x54'],
        ['55', '\x55'], ['56', '\x56'], ['57', '\x57'], ['58', '\x58'], ['59', '\x59'],
        ['5A', '\x5A'], ['5B', '\x5B'], ['5C', '\x5C'], ['5D', '\x5D'], ['5E', '\x5E'], ['5F', '\x5F'],
        ['60', '\x60'], ['61', '\x61'], ['62', '\x62'], ['63', '\x63'], ['64', '\x64'],
        ['65', '\x65'], ['66', '\x66'], ['67', '\x67'], ['68', '\x68'], ['69', '\x69'],
        ['6A', '\x6A'], ['6B', '\x6B'], ['6C', '\x6C'], ['6D', '\x6D'], ['6E', '\x6E'], ['6F', '\x6F'],
        ['70', '\x70'], ['71', '\x71'], ['72', '\x72'], ['73', '\x73'], ['74', '\x74'],
        ['75', '\x75'], ['76', '\x76'], ['77', '\x77'], ['78', '\x78'], ['79', '\x79'],
        ['7A', '\x7A'], ['7B', '\x7B'], ['7C', '\x7C'], ['7D', '\x7D'], ['7E', '\x7E'], ['7F', '\x7F'])

我在表中搜索字符串(例如'00')并将其替换为相应的条目('\ x00')。

这解决了我的问题,但我很确定有一种更清洁的方法。

© www.soinside.com 2019 - 2024. All rights reserved.