P Petkov
P Petkov

Reputation: 9

Hexadecimal to decimal converter

How could I make my program more efficient? Right now it is filled with if statements.

import copy
def hexadecimal(a):

   z = a.replace('0b','')

   y = z.rjust(8,'0')
   list1 = list(y)
   newlist = []
   for i in range(1,5):
      nig = list1.pop(0)
      newlist.append(nig)       

   if newlist == ['0','0','0','0']:
      valuenew = ''
   if newlist == ['0','0','0','1']:
      valuenew = '1'
   if newlist == ['0','0','1','0']:
      valuenew = '2'
   if newlist == ['0','1','0','0']:
      valuenew = '4'
   if newlist == ['1','0','0','0']:
      valuenew = '8'
   if newlist == ['0','0','1','1']:
      valuenew = '3'
   if newlist == ['0','1','1','1']:
      valuenew = '7'
   if newlist == ['0','1','0','1']:
      valuenew = '5'
   if newlist == ['0','1','1','0']:
      valuenew = '6'
   if newlist == ['1','0','0','1']:
      valuenew = '9'
   if newlist == ['1','0','1','0']:
      valuenew = 'A'
   if newlist == ['1','0','1','1']:
      valuenew = 'B'
   if newlist == ['1','1','0','0']:
      valuenew = 'C'
   if newlist == ['1','1','0','1']:
      valuenew = 'D'
   if newlist == ['1','1','1','0']:
      valuenew = 'E'
   if newlist == ['1','1','1','1']:
      valuenew = 'F'

   if list1 == ['0','0','0','0']:
      valuenew1 = ''
   if list1 == ['0','0','0','1']:
      valuenew1 = '1'
   if list1 == ['0','0','1','0']:
      valuenew1 = '2'
   if list1 == ['0','1','0','0']:
      valuenew1 = '4'
   if list1 == ['1','0','0','0']:
      valuenew1 = '8'
   if list1 == ['0','0','1','1']:
      valuenew1 = '3'
   if list1 == ['0','1','1','1']:
      valuenew1 = '7'
   if list1 == ['0','1','0','1']:
      valuenew1 = '5'    
   if list1 == ['0','1','1','0']:
      valuenew1 = '6'    
   if list1 == ['1','0','0','1']:
      valuenew1 = '9'    
   if list1 == ['1','0','1','0']:
      valuenew1 = 'A'    
   if list1 == ['1','0','1','1']:
      valuenew1 = 'B'    
   if list1 == ['1','1','0','0']:
      valuenew1 = 'C'    
   if list1 == ['1','1','0','1']:
      valuenew1 = 'D'    
   if list1 == ['1','1','1','0']:
      valuenew1 = 'E'    
   if list1 == ['1','1','1','1']:
      valuenew1 = 'F'

   print(valuenew + valuenew1)            

a = str(bin(int(input('enter a number'))))

hexadecimal(a)

The program is only meant to convert a binary up to 8 bits

Upvotes: 0

Views: 94

Answers (3)

Sven-Eric Krüger
Sven-Eric Krüger

Reputation: 1327

You can convert an arbitrary string literal that represents a number to an integer with given base and then convert that integer to a string.

>> b = '0b01001'
>> format(int(b, 2), 'X')
'9'
>> b = '1001'
>> format(int(b, 2), 'X')
'9'

CODE

def hexadecimal(a):
    return format(bin(a, 2), 'X')  # You could also print it

Upvotes: 0

fixatd
fixatd

Reputation: 1404

Python already has something built in for what you want hex(). However if you're hellbent on making your own, I've made some adjustments to your code:

(1) Since python doesn't have a switch statement the best you can do is to try to use a dict instead; I've also introduced a hex_digit function that is basically just referencing the value you need from HEX_DIGIT (you don't really need it but it abstracts that part if you need to replace it in the future)

(2) Adding some logic to your padding so it won't just be for 8 bit length numbers

HEX_DIGITS = {
  '0000': '',
  '0001': '1',
  '0010': '2',
  '0011': '3',
  '0100': '4',
  '0101': '5',
  '0110': '6',
  '0111': '7',
  '1000': '8',
  '1001': '9',
  '1010': 'A',
  '1011': 'B',
  '1100': 'C',
  '1101': 'D',
  '1110': 'E',
  '1111': 'F',
}

def hex_digit(val):
  print('Converting: ' , val)
  return HEX_DIGITS.get(val, '')

def hexadecimal(a):
  z = a.replace('0b','')
  len_z = len(z)

  # Add padding if len not a factor of 4
  if len_z % 4 != 0:
    full_length = len_z + (4 - (len_z % 4))
    z = z.rjust(full_length, '0')

  split_vals = [
    hex_digit(z[idx:idx+4])
    for idx in range(0,len(z),4)
  ]
  print("Hex value: {}".format("".join(split_vals)))

a = str(bin(int(input('Enter a number: '))))

hexadecimal(a)

Upvotes: 0

Eugene Yarmash
Eugene Yarmash

Reputation: 150128

How could I make my program more efficient?

You could simply use the format() or hex() function to convert an integer to a hex string:

>>> format(255, 'X')
'FF'
>>> hex(255)
'0xff'

Upvotes: 2

Related Questions