在字符串中附加十六进制字符

时间:2019-02-20 08:40:57

标签: python string hex character

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

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

  

“ 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”

有人有解决此问题的线索吗? 提前非常感谢

2 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为根据文档,不能将unicode字符串直接附加到字符串(utf-8)。

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

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

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

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

答案 1 :(得分: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')。

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