[pyar] Modulo dpkt

Federico Ferraro fferraro18 en gmail.com
Mar Ene 21 19:58:55 ART 2014


Muy pero muy interesante !!

Slds ...


El 21 de enero de 2014, 19:31, Alejandro Santos <listas en alejolp.com>escribió:

> 2014/1/21 Nicolas Rebagliati <nicolas.rebagliati en aenima-x.com.ar>:
> >
> >
> >>
> >>
> >> No entendí esto. ¿Querés intercambiar el primer nibble con el segundo
> >> nibble?
> >>
> >> >>> def swap_byte_nibble(x):
> >> ...     return (x << 4) & 0xf0 | (x >> 4) & 0xf
> >> ...
> >>
> >
> > Casi, el tema es que vos estas invirtiendo los hexadecimales.
> > Yo lo que necesito es obtener es obtener el hexadecimal invertido desde
> el
> > byte.
> >
>
> Por eso. La función swap_byte_nibble hace el swap de los nibbles a
> nivel integer. No toma strings, me pareció bastante feo hacerlo así.
> Si de todas formas querés strings podés hacer algo así:
>
> >>> def swap_nibbles_str(x):
> ...     return ''.join(reversed("%02x" % x))
> ...
> >>> swap_nibbles_str(0xf0)
> '0f'
> >>> swap_nibbles_str(0x0f)
> 'f0'
> >>> swap_nibbles_str(0x45)
> '54'
> >>> swap_nibbles_str(0x54)
> '45'
> >>> type(_)
> <type 'str'>
>
> En cambio con el ejemplo que te pasé antes:
>
> >>> def swap_byte_nibble(x):
> ...     return (x << 4) & 0xf0 | (x >> 4) & 0xf
> ...
> >>> swap_byte_nibble(0x45)
> 84
> >>> swap_byte_nibble(0x54)
> 69
> >>> type(_)
> <type 'int'>
>
>
> >
> > Te explico bien por si estoy mandando fruta.
> >
>
> Ojo que capaz estoy entendiendo fruta yo, eh! Por eso te pregunté,
> porque no termino de entender :P
>
> > yo tengo esto:
> > x = 'Ep\t\x00\x05\x02'
> >
> > x.encode('hex') -> '457009000502'
> > y yo lo que necesito obtener es: '54079000502'
> >
>
> ¿Performante? No tengo otra cosa más que comparar que lo te pasé. A
> ver qué sale...
>
> >>> timeit.timeit("[int(swap_byte_str(ord(w)), 16) for w in X]",
> setup="X='Ep\\t\\x00\\x05\\x02'\ndef swap_byte_str(x): return
> ''.join(reversed('%02x' % x))", number=1000000)
> 12.750516176223755
> >>> timeit.timeit("[swap_byte_nibble(ord(w)) for w in X]",
> setup="X='Ep\\t\\x00\\x05\\x02'\ndef swap_byte_nibble(x): return (x << 4) &
> 0xf0 | (x >> 4) & 0xf", number=1000000)
> 2.3403170108795166
>
>
> --
> Alejandro Santos
> _______________________________________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/listinfo/pyar
>
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
>
> La lista de PyAr esta Hosteada en USLA - Usuarios de Software Libre de
> Argentina - http://www.usla.org.ar
>



-- 
--
Federico Ferraro
Usuario Linux : 482533.
--
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20140121/04ec512f/attachment.html>


More information about the pyar mailing list