944,073 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 14538
  • Python RSS
Jun 18th, 2006
0

Add Hexadecimal numbers

Expand Post »
Is there a way to do math with hexadecimal numbers in Python?
Similar Threads
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jun 18th, 2006
0

Re: Add Hexadecimal numbers

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 ...
Python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: comparison
Next Thread in Python Forum Timeline: pywin32 MovefileEx





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC