| | |
Hexadecimal to Decimal (Python)
This snippet explores the change of a hexadecimal (base 16) string to a denary (base 10) integer and the reverse. Interestingly Python allows the representation of the hexadecimal string in two different forms. For instance 255 can be changed to 'FF' (also 'ff') or '0xff'. Any of these hexadecimal strings return the correct value of 255 again.
# change a hexadecimal string to decimal number and reverse # check two different representations of the hexadecimal string # negative values and zero are accepted # tested with Python24 vegaseat 11oct2005 def dec2hex(n): """return the hexadecimal string representation of integer n""" return "%X" % n def hex2dec(s): """return the integer value of a hexadecimal string s""" return int(s, 16) print "dec2hex(255) =", dec2hex(255) # FF print "hex2dec('FF') =", hex2dec('FF') # 255 print "hex(255) =", hex(255) # 0xff print "hex2dec('0xff') =", hex2dec('0xff') # 255
Similar Threads
- HELP!!! Converting Hexadecimal to Decimal or vice versa (MS SQL)
- hexadecimal to decimal (C)
- decimal to hexadecimal (C#)
- convert Decimal to Hexadecimal system.. (C)
- Decimal to Hexadecimal (Python)
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary digital dynamic events examples excel external feet file float format function gui handling hints homework iframe images import input java keycontrol line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton split string strings tails terminal text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable web-scrape whileloop word wxpython xlwt



