[Tutor] Converting MAC address . Need Help
Kent Johnson
kent37 at tds.net
Tue Feb 28 02:27:03 CET 2006
Travis Spencer wrote:
> 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'
This is not what Sudarshana asked for - '\\x00' and '\x00' are not the
same string - the first is a string of four characters - backslash,
letter x, digit 0, digit 0. The second is a string of one character with
the character value 0 - a NUL. Sudarshana wants to make a string of byte
values, you make a string of character representations of byte values.
Very different.
>>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?
Because he wants byte values not 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.
It all depends on where you want to end up :-)
There is actually a one-liner to do this but I didn't want to give it
all away.
Kent
More information about the Tutor
mailing list