Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Convert MAC to hex howto (http://www.velocityreviews.com/forums/t953141-re-convert-mac-to-hex-howto.html)

MRAB 10-07-2012 08:04 PM

Re: Convert MAC to hex howto
 
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(":"))

'\x00\x08\x9b\xce\xf5\xd4'



All times are GMT. The time now is 01:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57