[pyar] Modulo dpkt

Nicolas Rebagliati nicolas.rebagliati en aenima-x.com.ar
Mar Ene 21 16:03:10 ART 2014


>
> 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
> ...
> >>> hex(swap_byte_nibble(0xf0))
> '0xf'
> >>> hex(swap_byte_nibble(0x0f))
> '0xf0'
> >>> hex(swap_byte_nibble(0x54))
> '0x45'
> >>> hex(swap_byte_nibble(0x45))
> '0x54'
> >>> [hex(ord(x)) for x in S]
> ['0x45', '0x70', '0x9', '0x0', '0x5', '0x2']
> >>> [hex(swap_byte_nibble(ord(x))) for x in S]
> ['0x54', '0x7', '0x90', '0x0', '0x50', '0x20']
>
>
Casi, el tema es que vos estas invirtiendo los hexadecimales.
Yo lo que necesito es obtener es obtener el hexadecimal invertido desde el
byte.

Te explico bien por si estoy mandando fruta.

yo tengo esto:
x = 'Ep\t\x00\x05\x02'

x.encode('hex') -> '457009000502'
y yo lo que necesito obtener es: '54079000502'

podría invertir el string cada 2, pero eso me genera bardos con valores
como el 02, que invertido debería ser 2. Y de esa manera me daría 20.
seguramente hay algo mas performante que andar recorriendo el string
encodeado y darlos vuelta.
Pero no se me ocurre
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20140121/f303605c/attachment.html>


More information about the pyar mailing list