2

我想要从 get_node() 获得的 MAC 地址的正常格式。

我得到的格式是 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
4

1 回答 1

6

采用

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

更简洁地说,您可以通过使用消除临时touple变量

readableMACaddress = '-'.join('%02X' % ((myMAC >> 8*i) & 0xff) for i in reversed(xrange(6)))
于 2012-09-13T11:03:48.663 回答