Python为macaress获取正常的十六进制格式

2024-09-19 23:34:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个普通格式的MAC地址,我从get_node()获得。在

我得到的格式是0x0L0xdL0x60L0x76L0x31L0xd6L,我希望删除0x,并将L项转换为一个真正的十六进制数。应该是00-0D-60-76-31-D6。在

我怎么能意识到这一点?在

def getNetworkData (self):
    myHostname, myIP, myMAC =  AU.getHostname()

    touple1 = (myMAC & 0xFF0000000000) >> 40
    touple2 = (myMAC & 0x00FF00000000) >> 32
    touple3 = (myMAC & 0x0000FF000000) >> 24
    touple4 = (myMAC & 0x000000FF0000) >> 16
    touple5 = (myMAC & 0x00000000FF00) >> 8
    touple6 = (myMAC & 0x0000000000FF) >> 0

    readableMACadress = hex(touple1) + hex(touple2) + hex(touple3) + hex(touple4) + hex(touple5) + hex(touple6) 

    print readableMACadress

    return myHostname, myIP, readableMACadress

Tags: getmac地址格式hexmyipmyhostnamemymac
1条回答
网友
1楼 · 发布于 2024-09-19 23:34:43

使用

readableMACaddress = '%02X-%02X-%02X-%02X-%02X-%02X' % (touple1, touple2, touple3, touple4, touple5, touple6)

更简单地说,您可以使用

^{pr2}$

相关问题 更多 >