Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

how to separate hexadecimal

 
 
jrlen balane
Guest
Posts: n/a
 
      02-02-2005
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-02-2005
jrlen balane <(E-Mail Removed)> writes:
> 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


Assuming you really want hexa to begin with the characters '0x', the
string slicing way is:

a, b = hexa[2:4], hexa[4:6] # a = '87', b = 'BE'
c = int(a,16) + int(b, 16)

A more direct arithmetic way is:

x = int(hexa, 16) # x = the integer 0x87be
c = (x >> + (x & 0xff)
 
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
Re: how to separate hexadecimal jrlen balane Python 3 02-02-2005 01:01 PM
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