简体   繁体   中英

Convert a 4 bit binary nibble as a string into its hex equivalent value as a string

I'm not sure what I'm doing wrong, but my print statement doesn't seem to be working.

  def nib(string):
    if string == '0000':
        return '0'

    if string == '0001':
        return '1'

    if string == '0010':
        return '2'

    if string == '0011':
        return '3'

    if string == '0100':
        return '4'

    if string == '0101':
        return '5'

    if string == '0110':
        return '6'

    if string == '0111':
        return '7'

    if string == '1000':
        return '8'

    if string == '1001':
        return '9'

    if string == '1010':
        return "A"

    if string == '1011':
        return "B"

    if string == '1100':
        return "C"

    if string == '1101':
        return "D"

    if string == '1110':
        return "E"

    if string == '1111':
        return "F"

def main(nib):
    strings= input("Enter your 4 bit binary nibble, separated by spaces: ")
    strings= strings.split(' ')

    print(string)

I'd like to maintain the code in the simple format I have since I'm a beginner. Here are my instructions:

Write a function that takes as an argument, a 4 bit binary nibble represented as a string, and returns its hex equivalent value as a String

Use that function to write a program that accepts an entire string of binary nibbles entered from the keyboard and prints out the full hex equivalent. The nibbles in the input must be separated by spaces.

For Example:

An input of 0101 1101 produces an output of 5D An input of 0000 1001 0001 1111 1100 produces an output of 091FC

I don't know where I went wrong?????

There are a few things wrong with your code:

  1. You never call any of the functions you defined. The code inside a function is only executed when the function is called
  2. You don't do anything with your input values.

Your code to accept the input values is correct. I suggest you rename your variables to something that makes sense. Also your main function doesn't need a nib argument. The nib function is already defined in the global scope. Also, you don't need to specify an argument to .split if you want to split on spaces -- that is the default behavior.

At this point, you have a list of input nibbles, and you need to convert each of them. Once you convert each of them, you need to join the individual hex digits to a single string.

def main(): 
    user_in = input("Enter your 4 bit binary nibbles, separated by spaces: ") # e.g. 1000 0010 1010
    strings = user_in.split() # ["1000", "0010", "1010"]

    hex_digits = [nib(s) for s in strings] # ["8", "2", "A"]
    hex_value = "".join(hex_digits)        # "82A"
    print(hex_value)                       # Prints 82A

main() # You need to actually call the function to run it

Note: You can skip the need to define hex_digits and do hex_value directly like so: hex_value = "".join(nib(s) for s in strings)


Now, your nib function works, but you could clean it up a little to be more terse. You could define a dictionary which maps the binary values to the hex digits, and use that instead of a bunch of if statements (remember to fill in all the key-value pairs in bin_to_hex .

bin_to_hex = {"0000": "0", "0001": "1", "0002": "2", ..., "1001": "9", "1010": "A", ..., "1111": "F"}
def nib(n):
    return bin_to_hex[n]

You could even define bin_to_hex using a loop:

bin_to_hex = {f"{i:04b}": f"{i:x}".upper() for i in range(16)}

The f"..." construct is called the f-string syntax . :b formats an integer as a binary string, and :04 pads the result with zeros to a width of 4. :x formats an integer as a hex string. Doing this for all integers in the range(16) allows us to create the bin_to_hex without manually typing everything out.

This should be helpful.

def nib(string):
    if string == '0000':
        return '0'

    if string == '0001':
        return '1'

    if string == '0010':
        return '2'

    if string == '0011':
        return '3'

    if string == '0100':
        return '4'

    if string == '0101':
        return '5'

    if string == '0110':
        return '6'

    if string == '0111':
        return '7'

    if string == '1000':
        return '8'

    if string == '1001':
        return '9'

    if string == '1010':
        return "A"

    if string == '1011':
        return "B"

    if string == '1100':
        return "C"

    if string == '1101':
        return "D"

    if string == '1110':
        return "E"

    if string == '1111':
        return "F"


strings= input("Enter your 4 bit binary nibble, separated by spaces: ")
strings= strings.split(' ')
for s in strings:
    print(nib(s), end="")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM