Python get normal HEX format for MAC-Adress

…衆ロ難τιáo~ 提交于 2019-12-11 00:13:32

问题


I want a normal format for the MAC-Address I got from get_node().

The format I get is 0x0L0xdL0x60L0x76L0x31L0xd6L, I want the 0x deleted and the L-term to a real hex number. It should be 00-0D-60-76-31-D6 .

How can I realize this?

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

回答1:


Use

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

More concisely, you can eliminate the temporary touple variables by using

readableMACaddress = '-'.join('%02X' % ((myMAC >> 8*i) & 0xff) for i in reversed(xrange(6)))


来源:https://stackoverflow.com/questions/12404622/python-get-normal-hex-format-for-mac-adress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!