Networking in Python: Hexadecimal to Decimal Converter in Python

This is the first in a series about networking using Python and Python libraries to accomplish a myriad of networking goals. I’m currently working to build some networking tools so I want to share some of the tools I’m making.

This is a dead simple one. The goal here is to convert hexadecimal numbers into decimal numbers. Hexadecimals are used in computing and mathematics to represent numbers. They are made up usually of sixteen characters, which is why there are called base 16 numbers. The characters are the numbers 0-9 and A-F. They can often times be used to extrapolate data from websites, specifically they can be used to find IPv4 addresses as they convert to base 10 numbers which are simple 0-9.

We can actually do this with the use of an ASCII chart. However, we become programmers so we can make the tools do accomplish our goals. Below is an example of a pretty detailed ASCII conversion chart.


Below is our code:


import os

def HexConvert():
	s = raw_input("Please put in the hex number you wish to convert: ")
	# converts to base 16
		convert = int(s, 16)
		print "This was not a hex value"

	print("Here is your decimal " + str(convert))


First we go ahead and import our OS library, which we use to clear our terminal screen. After that we need to ask the user for the hexadecimal number they want to convert. We take that and throw it into a try statement. I put it in a try statement so we can scrub the user input. If a hex value is not put into the converter it will throw the the exception message.

All the work is done via the int() function. The function itself defaults to converting a string or number into an integer value. However if we add our hex number into the function with the argument 16 it will convert our hex number into a base 10 integer value. We simply add in a print statement while converting our number back to a string.  It’s as simple as that.

Share Me If You Liked Me!Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *