Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert MAC to hex howto

Reply
Thread Tools

Convert MAC to hex howto

 
 
Johannes Graumann
Guest
Posts: n/a
 
      10-07-2012
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

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      10-07-2012
Johannes Graumann <> 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(':'))
 
Reply With Quote
 
 
 
 
Johannes Graumann
Guest
Posts: n/a
 
      10-07-2012
Paul Rubin wrote:

> Johannes Graumann <> 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(':'))


Thanks to you as well!

Joh

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert MAC to hex howto Peter Otten Python 0 10-07-2012 09:01 PM
Re: Convert MAC to hex howto Johannes Graumann Python 0 10-07-2012 08:38 PM
Re: Convert MAC to hex howto MRAB Python 0 10-07-2012 08:04 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



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