Add Hexadecimal numbers

Thread Solved

Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Add Hexadecimal numbers

 
0
  #1
Jun 18th, 2006
Is there a way to do math with hexadecimal numbers in Python?
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Add Hexadecimal numbers

 
0
  #2
Jun 18th, 2006
Since hexadecimal numbers are really strings representing a numeric value, you best convert these strings to a denary (base 10) number, do the mathematical operation and change the result back to the hexadecimal. Here is an example ...
  1. def add_hex2(hex1, hex2):
  2. """add two hexadecimal string values and return as such"""
  3. return hex(int(hex1, 16) + int(hex2, 16))
  4.  
  5. print add_hex2('0xff', '0xff') # 0x1fe
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC