0

hexadecimal to decimal in python(Easy Solution)

hexadecimal to decimal in python

hexadecimal string to a decimal value in python – Write a function that converts the hexadecimal string to a decimal value. You can NOT use any Python library/module to help you do it. Your function name should be named hex and take one argument as the input.

In mathematics and calculations, the hexadecimal number system (also 16 or hexadecimal) is a positional number system that uses the base 16 to represent numbers. Unlike the usual way of using 10 symbols to represent numbers, hexadecimal uses 16 different symbols. In most cases, the symbols “0” – “9” represent values ​​from 0 to 9 and “A” – “F” (or “A” – “f”) represent values ​​from 10 to 15.

The decimal numeral system (also called the base-ten positional numeral system, and occasionally called denary/ˈdiːnəri/[1] or decanary) is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers of the Hindu–Arabic numeral system.[2] The way of denoting numbers in the decimal system is often referred to as decimal notation.[3]

For example, if the input is “F23A”, the decminal value is 62010 and the procedure in the conversion is at below:

QUESTION 2

Hints: you can use int('A', 16) to convert a hex string value to dec integer. Also, you can use funtion len(input) to find out the length of the string. You can extract a particular character in a string by specifying an index.

# Below codes are hints
# exmaple

a = "F23A"
print(len(a))
print(a[3])
print(int(a[3],16))
4
A
10

In [8]:

# Implementation

input = "F23A"

# Start the code below
def hex(s):
    l = len(s) - 1
    d = 0
    for c in s:
        res = int(c,16)*(16**l)
        d += res
        l -= 1
    return d
        
  

In [9]:

# test your code here
input = "F23A"
hex(input)

Out[9]:

62010

If you have another efficient method, please share the comment section.

If you also use Python or any other language to do your work(hexadecimal string to a decimal value in python). Share it in the comment section to help others

keep visiting : shaadcoding

shaad

Leave a Reply

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