简体   繁体   English

使用转义字符'\\ x'创建字符串并在python中输入用户

[英]Create a string with escape characters '\x' and user input in python

I am trying to create a string with user input with '\\x' escape character 我试图用用户输入的'\\ x'转义字符创建一个字符串

I am doing something like this - 我正在做这样的事情-

    def create_hex_string(a, b): # a and b are integers of range (0-255)
        # convert decimal to hex and pad it with 0
        # for eg: 10 -> 0x0a
        ahex = "{0:#0{1}x}".format(register,4)
        bhex = "{0:#0{1}x}".format(value,4)
        str = "\x08\x{}\x00\x{}".format(ahex[2:], bhex[2:])
        return str

When I try to execute this, the escape characters are no more relevant and it gives me an error. 当我尝试执行此操作时,转义字符不再相关,这给了我一个错误。

I also tried to use literal strings to create my hex string with user inputs, something like - 我还尝试使用文字字符串通过用户输入来创建十六进制字符串,例如-

str = r'\x08\x{}\x00\x{}'.format(ahex[2:], bhex[2:])

But I don't find a way to convert a literal string back to a non literal one which can identify the escape characters. 但是我找不到一种将文字字符串转换回可以识别转义字符的非文字字符串的方法。

I also tried to see how re.escape() works, but it escapes all characters except ASCII. 我还尝试查看re.escape()的工作方式,但是它转义了除ASCII之外的所有字符。 Any pointers regarding this would be really appreciated. 任何有关此的指针将不胜感激。


Better explanation 更好的解释

I have a peripheral hardware device with registers. 我有一个带寄存器的外围硬件设备。 I can set a specific value in a specific register in the device over a socket. 我可以通过套接字在设备的特定寄存器中设置特定值。

sock.send("\x80\x01")

This command only works if it is a double quoted string, so the \\x escape character has a meaning. 该命令仅在用双引号引起来的字符串时才有效,因此\\ x转义符具有含义。 The command above would set register 128 to value 1 because- 0x80 = 128 0x01 = 1 上面的命令会将寄存器128设置为值1,因为-0x80 = 128 0x01 = 1

On that line of thought, I created a function 根据这种思路,我创建了一个函数

1   def create_hex_string(register, value): # a and b are integers of range (0-255)
2       # funky stuff to convert register and value to hex.
3       # reghex = hex value of register (128 = 80 in hex)
4       # valhex = hex value of value (1 = 01 in hex)
5       str = "\x{}\x{}".format(reghex, valhex)
6   return str

7   cmd = create_hex_string(128, 1)
8   sock.send(cmd)

If you see, line 5 would give an error, it does not accept. 如果看到的话,第5行会给出错误,它不会接受。 Instead of double quoted string in line 7, I used a literal string with format. 而不是在第7行中用双引号引起来的字符串,我使用了带格式的文字字符串。

5    str = r'\x{}\x{}'.format(reghex, valhex)

With this I lost the significance of the escape characters. 这样我就失去了转义字符的意义。

I hope this helps to understand the problem better. 我希望这有助于更好地理解问题。 Let me if I missed something, and I will add that in the next edit. 如果我错过了什么,请允许我,我将在下一次编辑中添加它。

You can create a one-character string from a integer value using the chr() function. 您可以使用chr()函数从整数值创建一个单字符字符串。

Is this what you are trying to do? 这是您要做什么?

def create_hex_string(a, b): # a and b are integers of range (0-255)
    return "\x08{}\x00{}".format(chr(a), chr(b))

s = create_hex_string(65, 66)
assert s[0] == '\x08'
assert s[1] == 'A'
assert s[2] == '\x00'
assert s[3] == 'B'
assert s == "\x08\x41\x00\x42"

# The example from OP's comment:
assert create_hex_string(128, 1) == "\x08\x80\x00\x01"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM