Is this the efficent way?

Carsten Gaebler cg at schlund.de
Fri Jul 28 10:35:40 EDT 2000


"Arinté" wrote:
> 
> I need to split a double (8) byte character into two bytes to send as a
> string so I have this code
> def hexSplit(arg):
>  ans = 0
>  lobyte =arg-256
>  if arg<=255:
>   ans = "\x00" + chr(arg)
>  else:
>   hibyte = lobyte/256
>   if (lobyte%256)>0:
>    hibyte = hibyte+1
>   ans = chr(hibyte)+chr(lobyte)
>  return ans
> 
> Is this good enough?

I think this is better:

def hexsplit(arg):
        lobyte =  arg & 0x00FF
        hibyte = (arg & 0xFF00) >> 8
        return chr(hibyte) + chr(lobyte)


Carsten.



More information about the Python-list mailing list