Creating a 2s compliment hex string for negitive numbers

bSneddon w.g.sneddon at gmail.com
Fri Aug 1 17:25:40 EDT 2014


On Friday, August 1, 2014 4:47:20 PM UTC-4, MRAB wrote:
> On 2014-08-01 21:35, bSneddon wrote:
> 
> > I need to calculate an error correction code for an old protocol.
> 
> >
> 
> > I calculate the integer 4617 and want to code the 2s compliment in ASCII
> 
> > hex EDF7.  When issue the following.
> 
> >>>> hex(-4617)
> 
> > '-0x1209'
> 
> >
> 
> > Does anyone know a clean way to get to the desired results?   My ECC will always
> 
> > be 16 bit (4 nibble) hex number.
> 
> >
> 
> Use a bitwise AND:
> 
> 
> 
>  >>> hex(-4617 & 0xFFFF)
> 
> '0xedf7'

Thanks



More information about the Python-list mailing list