Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to separate hexadecimal

Reply
Thread Tools

Re: how to separate hexadecimal

 
 
jrlen balane
Guest
Posts: n/a
 
      02-02-2005
On Wed, 02 Feb 2005 17:36:04 +1000, Nick Coghlan <(E-Mail Removed)> wrote:
> jrlen balane wrote:
> > i have a 4 digit hex number (2 bytes) and i want to separate it into 2
> > digit hex (1 byte each) meaning i want to get the upper byte and the
> > lower byte since i am going to add this two.
> > how am i going to do this?
> > should i treat it just like a normal string?
> > please help, thanks.
> >
> > ex. hexa = '0x87BE" # what i want to do is:
> > a = 0x87, b = 0xBE # so that i could do this:
> > c = a + b #which should be equal to 0x145

>
> divmod does what you want:
>
> Py> val = 0x87be
> Py> hi, lo = divmod(val, 0x100)
> Py> hex(hi), hex(lo)
> ('0x87', '0xbe')
> Py> hex(hi + lo)
> '0x145'
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
> ---------------------------------------------------------------
> http://boredomandlaziness.skystorm.net
> --
> http://mail.python.org/mailman/listinfo/python-list
>

would i be able to perform bitwise operation with the result?
say, i want to get the two's complement of the result, is this correct:

twos_complement = (~ hex(hi + lo)) + 1
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-02-2005
jrlen balane <(E-Mail Removed)> writes:
> would i be able to perform bitwise operation with the result?
> say, i want to get the two's complement of the result, is this correct:
>
> twos_complement = (~ hex(hi + lo)) + 1


You can do bit operations, but hex(n) is the hex string for n, which
is not what you want.

If you want the hex form of the two's complement, just say

hex(-(hi+lo) & 0xff)

assuming you want one byte, or

hex(-(hi+lo) & 0xffff)

for two bytes.
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      02-02-2005
Paul Rubin wrote:
> jrlen balane <(E-Mail Removed)> writes:
>
>>would i be able to perform bitwise operation with the result?
>>say, i want to get the two's complement of the result, is this correct:
>>
>>twos_complement = (~ hex(hi + lo)) + 1

>
>
> You can do bit operations, but hex(n) is the hex string for n, which
> is not what you want.
>
> If you want the hex form of the two's complement, just say
>
> hex(-(hi+lo) & 0xff)
>
> assuming you want one byte, or
>
> hex(-(hi+lo) & 0xffff)
>
> for two bytes.


More generally though, the "flip the bits and add one" of two's complement is
just a hardware implementation trick for "2*n - x".

When not working at the hardware level, just go with the definition:

Py> def complement(val, limit=256):
.... if val >= limit or val < 0:
.... raise ValueError("Value out of range for complemented format")
.... if val == 0:
.... return 0
.... return limit - val
....
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(0x55, 256*256))
'0xffab'

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      02-02-2005
Nick Coghlan wrote:
> When not working at the hardware level, just go with the definition:
>
> Py> def complement(val, limit=256):
> ... if val >= limit or val < 0:
> ... raise ValueError("Value out of range for complemented format")
> ... if val == 0:
> ... return 0
> ... return limit - val
> ...
> Py> hex(complement(0x55))
> '0xab'
> Py> hex(complement(0x55, 256*256))
> '0xffab'


Or there's the simplest definition:

Py> def complement(val, limit=256):
.... return (limit - val) % limit
....
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(-0x55))
'0x55'
Py> hex(complement(0xab))
'0x55'
Py> hex(complement(-0xab))
'0xab'

Cheers,
Nick.

--
Nick Coghlan | (E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
how to separate hexadecimal jrlen balane Python 1 02-02-2005 07:35 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments