1

I have another problem with a program that converts binary digits to hexadecimal. I have the program that runs well but displays the hexadecimal digit in small caps though the answer is required to be in caps as shown in the question and sample run

This is my code

def binaryToHex(binaryValue):
#convert binaryValue to decimal

decvalue = 0
for i in range(len(binaryValue)):
    digit = binaryValue.pop()
    if digit == '1':
        decvalue = decvalue + pow(2, i)

#convert decimal to hexadecimal
hexadecimal=hex(decvalue)
return hexadecimal
def main():
  binaryValue = list(input("Input a binary number: "))
  hexval=binaryToHex(binaryValue)

  hexa=h1.capitalize() #Tried to use capitalize() function but didn't worl
  print("The hex value is",hexa[ 2:4]) #strips off the first 2 digits
main()

This is what is displayed when I run

3
  • Use upper instead of capitalize. Commented Mar 21, 2019 at 7:50
  • umm hexa[ 2:4] doesn't just strip off the first two digits... that'd be [2:]... also, I take it though this is an exercise of some sort and you're not allowed to just use: format(int(binaryValue, 2), 'X') ? Commented Mar 21, 2019 at 7:55
  • @JonClements I don't think there is any restriction for that. I can use it Commented Mar 21, 2019 at 8:05

3 Answers 3

1

Since this comes up a fair bit - here's an answer that's fairly Pythonic and hopefully serves as a canonical reference for future questions.

First off, just keep the input as a string:

binary_value = input('Enter a binary number: ')

Then use the builtin int with a base argument of 2 (which indicates to interpret the string as binary digits) to get an integer from your string:

number = int(binary_value, 2)
# 10001111 -> 143

Then you can use an f-string to print your number with a format specifier X which means in "hex with upper case letters and no prefix":

print(f'The hex value is {number:X}')

Your entire code base would then be something like (sticking with two-functions and your naming conventions):

def binaryToHex(binaryValue):
    number = int(binaryValue, 2)
    return format(number, 'X')

def main():
    binaryValue = input('Enter a binary number: ')
    print('The hex value is', binaryToHex(binaryValue))

main()
Sign up to request clarification or add additional context in comments.

4 Comments

I have changed my code to def binaryToHex(binaryValue): decvalue=int(binaryValue,2) hexadecimal=hex(decvalue) return hexadecimal def main(): binaryValue = input("Input a binary number: ") hexval=binaryToHex(binaryValue) hexa=hexval.upper() print(f'The hex value is {hexval:X}') main()
I get an error that unknown format code 'X' for object of type 'str'
@PatienceMuthoki Yes... because you want don't want to do any other string conversations, you literally want to print the int you get with the format specifier X and it does all that for you. You literally only need the 3 lines above... you don't need to do anything else with hex or anything.
So 1) get the input, 2) convert it to an integer, 3) print that number with the format specifier
0

One mistake you've made is h1 does not exist in the code and yet it's present.

.upper() on a string changes it to uppercase

def main():
    binaryValue = list(input("Input a binary number: "))
    hexval=binaryToHex(binaryValue)
    hexa=hexval.upper() 
    print("The hex value is",hexa[ 2:4]) #strips off the first 2 digits

output:

Input a binary number: 10001111
The hex value is 8F

2 Comments

Thanks it has worked on my local machine though the program used for marking indicates an error that what the standard output isn't what was expected
Could you post the error message. I am unable to understand the issue you are facing
0

just make one function...

def binaryToHex():
    binval = input('Input a binary number : ')
    num = int(binval, base=2)
    hexa = hex(num).upper().lstrip('0X')
    print(f'The hex value is {hexa}')

4 Comments

Have you tried this with an input of 00000000 - .lstrip doesn't do what you think it does here...
Sure i've tried what @Wira Bhakti has done and doesn't display the correct thing. Furthermore we are required to use 2 functions
@JonClements can you explain why 'X0X'.lsplit('X0') interpreted as a "" (blank string)...?
@Wira .lstrip removes all characters (not the entire string itself) in the string given from the start of the string until it encounters a character that's not in the characters given.... hence... 'ABCABCABC'.lstrip('BCA') will give you an empty string, but 'ABCABCDABC'.lstrip('BCA') will give you 'DABC'...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.