[Tutor] Converting MAC address . Need Help
Travis Spencer
travislspencer at gmail.com
Tue Feb 28 02:19:24 CET 2006
On 2/27/06, Kent Johnson <kent37 at tds.net> wrote:
> Sudarshana KS wrote:
> > The problem i am facing is the mac address i have is in the form
> > 00:11:22:33:44:55 need to convert to '\x00\x11\x22\x33\x44\x55'
Perhaps I'm mistaking but it seems that you need to prepend the `\x'
escape sequence to the mac address you already have and replace the
colons with the same sequence. If it is any harder than that, I'm
missing something. If I got what you mean, Sudarshana, you can
achieve your goal with this one-liner:
>>> macAddress = '00:11:22:33:44:55'
>>> reduce(lambda a, b: a + r"\x" + b, macAddress.split(':'), "")
'\\x00\\x11\\x22\\x33\\x44\\x55'
Perhaps you may prefer this:
>>> "\\x" + macAddress.replace(":", "\\x")
'\\x00\\x11\\x22\\x33\\x44\\x55'
> The list elements are still strings, you need to convert them to
> integers.
Why is this needed, Kent? Why not just leave them as strings?
> The strings are in hexadecimal so use the optional base
> parameter to int():
> >>> int('11', 16)
> 17
>
> Finally you have to convert this number to a byte of a string using chr():
> >>> chr(17)
> '\x11'
Seems like a lot of extra work to me.
--
Regards,
Travis Spencer
More information about the Tutor
mailing list