Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert MAC to hex howto

19 views
Skip to first unread message

Johannes Graumann

unread,
Oct 7, 2012, 3:44:38 PM10/7/12
to pytho...@python.org
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

Paul Rubin

unread,
Oct 7, 2012, 3:56:07 PM10/7/12
to
Johannes Graumann <johannes...@web.de> writes:
> '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(':'))

MRAB

unread,
Oct 7, 2012, 4:04:38 PM10/7/12
to pytho...@python.org
On 2012-10-07 20:44, Johannes Graumann wrote:
> 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.
>
It looks like you're using Python 2, so:

>>> s = '00:08:9b:ce:f5:d4'
>>> "".join(chr(int(x, 16)) for x in s.split(":"))

Johannes Graumann

unread,
Oct 7, 2012, 4:38:26 PM10/7/12
to pytho...@python.org
Many thanks!

Joh

Johannes Graumann

unread,
Oct 7, 2012, 4:38:39 PM10/7/12
to pytho...@python.org
Thanks to you as well!

Joh

Peter Otten

unread,
Oct 7, 2012, 5:01:17 PM10/7/12
to pytho...@python.org
Johannes Graumann wrote:

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

>>> import binascii
>>> expectedservermac = '00:08:9b:ce:f5:d4'
>>> binascii.unhexlify(expectedservermac.replace(":", ""))
0 new messages