Convert MAC to hex howto

  • Thread starter Johannes Graumann
  • Start date
J

Johannes Graumann

Dear all,

I'm trying to convert
'00:08:9b:ce:f5:d4'
to
'\x00\x08\x9b\xce\xf5\xd4'
for use in magic packet construction for WakeOnLan like so:
wolsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
wolsocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
wolsocket.sendto('\xff'*6 + '\x00\x08\x9b\xce\xf5\xd4'*16,
(expectedlanbroadcast, 80))

This is what I came up whith, but I remain puzzled on how to preserver the
leading '\h' ... thanks for any hint

expectedservermac = '00:08:9b:ce:f5:d4'
hexcall = str.split(expectedservermac,":")
hexcall.insert(0,'')
hexcall = "\\x".join(hexcall).decode('string_escape')

Thanks for any pointers.

Sincerely, Joh
 
P

Paul Rubin

Johannes Graumann said:
'00:08:9b:ce:f5:d4'
...
hexcall = "\\x".join(hexcall).decode('string_escape')

I think it's best not to mess with stuff like that. Convert to integers
then convert back:

mac = '00:08:9b:ce:f5:d4'
hexcall = ''.join(chr(int(c,16)) for c in mac.split(':'))
 
J

Johannes Graumann

Paul said:
I think it's best not to mess with stuff like that. Convert to integers
then convert back:

mac = '00:08:9b:ce:f5:d4'
hexcall = ''.join(chr(int(c,16)) for c in mac.split(':'))

Thanks to you as well!

Joh
 

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top