简体   繁体   中英

Python string formatting for number system conversion.

Given an integer,n, print the following values for each integer from to :

  1. Decimal
  2. Octal
  3. Hexadecimal (capitalized)
  4. Binary The four values must be printed on a single line in the order specified above for each i from 1 to n. Each value should be space-padded to match the width of the binary value of n.

CODE:

def print_formatted(number):
    for i in range(1,number + 1):
        decimal = i
        print (decimal," ",end='')
        d = decimal

        octal = ""
        while d != 0:
            r = d % 8
            d = d // 8
            rem = str(r)
            octal = rem + octal
        print (octal," ",end='')

        d = decimal
        hexadecimal = ""
        while d != 0:
            r = d % 16
            d = d // 16
            rem = str(r)
            c = r - 9
            if c > 0:
                if c == 1:
                    rem = 'A'
                elif c == 2:
                    rem = 'B'
                elif c == 3:
                    rem = 'C'
                elif c == 4:
                    rem = 'D'
                elif c == 5:
                    rem = 'E'
                elif c == 6:
                    rem = 'F'

            hexadecimal = rem + hexadecimal
        print (hexadecimal," ",end='')

        d = decimal
        binary = ""
        while d != 0:
            r = d % 2
            d = d // 2
            rem = str(r)
            binary = rem + binary
        print (binary)
if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

Your Output (stdout)

1 1 1 1

2 2 2 10

Expected Output

The '0' in the second line should come under the last '1'. Basically, the binary should be printed from right to left with the rightmost position being that of the last digit of the longest binary.

Hexadecimal, octal and binary values can be converted from decimal using the python functions hex() , oct() and bin() respectively.

The format function can output the numbers hex, oct and bin depending on the arguments you pass to it.

So you could do something like

def print_formatted(number):
    # "": decimal
    # x: hexadecimal
    # o: octal
    # b: binary
    bases = ["", "x", "o", "b"]
    numbers_formatted = [format(number, x).upper() for x in bases]

    return " ".join(numbers_formatted)

when used as

print(print_formatted(11))

outputs

# 11 B 13 1011

You can checkout https://docs.python.org/3.5/library/string.html#formatspec for more details.

You need to be carefull with the text alignments in your output. Also you can use the inbuilt python functions

def print_formatted(number):    
    width=len(str(bin(number)))-2
    for i in range(1, number + 1):
        print("%s %s %s %s" % (str(i).rjust(width, " "), str(oct(i))[2:].rjust(width, " "), hex(i)[2:].rjust(width, " ").upper(), bin(i)[2:].rjust(width, " ")))

Use Oct Hex and Bin functions. Covert them to strings. Trim the base notations. User rjust for text alignment

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