(Pdb) p mac
'\xd0\xbf\x9c\xd8\xf0\x00'
p type(mac)
<type 'str'>
Using Python2.7, how can I extract and print the exact MAC-address? In .proto message, mac_address is defined as "optional bytes mac".
(Pdb) p mac
'\xd0\xbf\x9c\xd8\xf0\x00'
p type(mac)
<type 'str'>
Using Python2.7, how can I extract and print the exact MAC-address? In .proto message, mac_address is defined as "optional bytes mac".
import struct
a='\xd0\xbf\x9c\xd8\xf0\x00'
for i in range(6):
print hex(struct.unpack("B", a[i])[0])[2:], ":",
prints
d0 : bf : 9c : d8 : f0 : 0 :
This perhaps?
>>> mac_str = ':'.join("{:02x}".format(ord(c)) for c in mac)
>>> print(mac_str)
d0:bf:9c:d8:f0:00
If you just want the list of ints do:
mac_decoded = [ord(c) for c in mac]
Not sure what else you mean by 'decoded'...
':'.join([hex(c)[2:] for c in map(ord,'\xd0\xbf\x9c\xd8\xf0\x00')])
Prints:
'd0:bf:9c:d8:f0:0'
Basically, this line converts the hex string into a list of 0-to-255 range ints and then convert each element c back into a hex string without the '\x' characters and then join the list elements as a string with a ':' in-between.