0
(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".

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
user7375520
  • 273
  • 2
  • 15
  • Isn't there a way to decode it and see the contents. Why people are going into string formatting.which is not what I am looking for.. – user7375520 May 06 '17 at 12:43
  • Maybe you could show what your desired result would look like for your given example string, since 'decode' could mean many different things. – PaulMcG May 06 '17 at 13:24

3 Answers3

0
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 :
dede
  • 706
  • 9
  • 19
  • Not like this. I mean the protobuf that I received as some variables as strings and some mac.. When I print it, all variables which are defined in .proto as ints, strings are coming just fine, except for the one whose type is bytes. Isn't there a more generic way to display. – user7375520 May 06 '17 at 11:54
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'...

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
0
':'.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.