字符串到二进制python

use*_*508 1 python networking byte

这对你们大多数人来说可能是一个虚假的问题,但我正面临这个问题;

我用scapy捕获网络上的mac地址,我将这个mac地址转换为"\ x00\x21\x23\x24\x25"这样:

...
  b = p.sprintf("%Dot11.addr2%")
  print "Sending to this MAC Address",b
  #Here, the mac address looks like 00:00:00:00:00:00
  dstmac = str('\\x'+b.replace(':','\\x'))
  #Now, the mac address looks like this:
  "\x00\x00\x00\x00\x00\x00"
  sendp(dst=dstmac,blablablabla)
...
Run Code Online (Sandbox Code Playgroud)

当此脚本在线路上发送mac时,它会发送以十六进制编码的实际\ x(以及所有后续数字),而不是发送00 00 00 00 00 00.

通常,你可以这样使用它,它很好:"sendp(dst ="\ x00\x00\x00\x00\x00\x00",blablablabla)

我认为我在做这个字符串操作或编码时做错了.

任何输入都有帮助!

谢谢 !

Ign*_*ams 6

\x标记是作为字符串文字.将字符串解码为十六进制.

>>> '123456789abc'.decode('hex')
'\x124Vx\x9a\xbc'
Run Code Online (Sandbox Code Playgroud)