Skip to main content
 首页 » 编程设计

python之字符串到二进制python

2025年05月04日27insus

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

我正在用 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) 
... 

当此脚本通过网络发送 mac 时,它会发送以十六进制编码的实际\x(以及所有后续数字),而不是发送 00 00 00 00 00 00。

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

我认为我在进行此字符串操作或编码时做错了什么。

对此的任何输入都会有所帮助!

谢谢!

请您参考如下方法:

\x 符号用于字符串文字。而是将字符串解码为十六进制。

>>> '123456789abc'.decode('hex') 
'\x124Vx\x9a\xbc'