Python 获取 MAC 地址的正常 HEX 格式

标签 python macos format hex mac-address

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

我得到的格式是 0x0L0xdL0x60L0x76L0x31L0xd6L,我希望删除 0x 并将 L-term 改为真正的十六进制数。它应该是 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

最佳答案

使用

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)))

关于Python 获取 MAC 地址的正常 HEX 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12404622/

相关文章:

python - Django_beanstalkd 中的连接被拒绝

python - 类型错误 : unsupported format string when printing Pandas dataframe with incorrectly shaped index

python - 如何将多个 Python 源文件连接成一个文件?

macos - XPC 服务的替代方案

ios - SwiftUI 中默认展开的 List 或 OutlineGroup

python - 如何在 python 中的 pickle.load() 之后关闭文件

在 RxSwift 中快速使用未解析的标识符 `create`

php - 将数字转换成xx.xx万格式?

Java String.format 两位小数,以点作为千位分隔符,以逗号作为小数分隔符

java - 如何在Java中将这个日期格式 "Wednesday, 12 June 2019 14:23:39"解析为这个 "2019-03-05T11:56:13Z"?