将十六进制MAC地址转换为用户可读格式(Python)(Convert Hexadecimal MAC address to user readable formatting (Python))
我从套接字收到这种格式的MAC地址:0024e865a023(十六进制转换为二进制,带有received-string.encode(“hex”))
我想将其转换为用户可读格式,如:00-24-e8-65-a0-23
有什么简单的方法吗?
I'm receiving from the socket a MAC address in this format: 0024e865a023 (hex converted from binary with received-string.encode("hex"))
I would like to convert it to a user readable format like this : 00-24-e8-65-a0-23
Any easy way to do so?
最满意答案
您可以将MAC地址拆分为每个块的数组,然后将它们连接到- :
mac = '0024e865a023' blocks = [mac[x:x+2] for x in xrange(0, len(mac), 2)] macFormatted = '-'.join(blocks)You can break apart the MAC address into an array of each block, and then join them on -:
mac = '0024e865a023' blocks = [mac[x:x+2] for x in xrange(0, len(mac), 2)] macFormatted = '-'.join(blocks)更多推荐
发布评论