【问题标题】:How to use Python to convert an octal to a decimal如何使用 Python 将八进制转换为十进制
【发布时间】:2016-05-28 19:04:24
【问题描述】:

我有一个小小的家庭作业,我需要将十进制转换为八进制,然后将八进制转换为十进制。我做了第一部分,却想不出第二部分来挽救我的生命。第一部分是这样的:

decimal = int(input("Enter a decimal integer greater than 0: "))

print("Quotient Remainder Octal")
bstring = " "
while decimal > 0:
    remainder = decimal % 8
    decimal = decimal // 8
    bstring = str(remainder) + bstring
    print ("%5d%8d%12s" % (decimal, remainder, bstring))
print("The octal representation is", bstring)

我在这里阅读了如何转换它:Octal to Decimal,但我不知道如何将其转换为代码。

【问题讨论】:

  • 到目前为止你尝试过什么?付出一些努力,SO 不是代码编写服务。

标签: python decimal octal


【解决方案1】:

从十进制到八进制:

oct(42) # '052'

八进制转十进制

int('052', 8) # 42

如果您想将八进制作为字符串返回,那么您可能需要将其包装在 str 中。

【讨论】:

    【解决方案2】:

    这些第一行采用任何十进制数并将其转换为任何所需的基数

    def dec2base():
        a = int(input('Enter decimal number: \t'))
        d = int(input('Enter expected base: \t'))
        b = ""
        while a != 0:
            x = '0123456789ABCDEF'
            c = a % d
            c1 = x[c]
            b = str(c1) + b
            a = int(a // d)
        return (b)
    

    第二行做同样的事情,但给定的范围和给定的小数

    def dec2base_R():
        a = int(input('Enter start decimal number:\t'))
        e = int(input('Enter end decimal number:\t'))
        d = int(input('Enter expected base:\t'))
        for i in range (a, e):
            b = ""
            while i != 0:
                x = '0123456789ABCDEF'
                c = i % d
                c1 = x[c]
                b = str(c1) + b
                i = int(i // d)
        return (b)
    

    第三行从任意进制转换回十进制

    def todec():
        c = int(input('Enter base of the number to convert to decimal:\t'))
        a = (input('Then enter the number:\t ')).upper()
        b = list(a)
        s = 0
        x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        for pos, digit in enumerate(b[-1::-1]):
            y = x.index(digit)
            if int(y)/c >= 1:
                print('Invalid input!!!')
                break
            s = (int(y) * (c**pos)) + s
        return (s)
    

    注意:如果有人需要,我也有 GUI 版本

    【讨论】:

    • 非常好!谢谢
    【解决方案3】:
    def decimal_to_octal(num1):
      new_list = []
      while num1 >= 1:
        num1 = num1/8
        splited = str(num1).split('.')
        num1 = int(splited[0])
        appendednum = float('0.'+splited[1])*8
        new_list.append(int(appendednum))
        decimal_to_octal(num1)    
      return "your number in octal: "+''.join(str(v) for v in new_list[::-1])
    
    print(decimal_to_octal(384))
    

    【讨论】:

    • 欢迎添加一些 cmets 来解释为什么这会回答原始发帖人的问题。
    • 和你之前的回答一模一样。
    • 解释一下。
    【解决方案4】:

    我不是 Python 方面的专家……但我写了这个逻辑……它工作得很好。

    def octToDec(oct):
        lenOct = str(oct)
        le = len(lenOct)
        octal = 0
        for i in (range(le)):
            octal = octal + int(lenOct[i])* pow(8, le-1)
            le -= 1
        print(octal)
    
    octToDec(number)
    

    【讨论】:

      【解决方案5】:
      def decimalToOctal(num1):
        new_list = []
        while num1 >= 1:
          num1 = num1/8
          splited = str(num1).split('.')
          num1 = int(splited[0])
          appendednum = float('0.' + splited[1])*8
          new_list.append(int(appendednum))
          decimalToOctal(num1)
        return "your number in octal: " + ''.join(str(v) for v in new_list[::-1])
      
      print(decimalToOctal(384))
      

      更简单的版本:

      a = input("Enter a string of octal digits: ")
      while a != 0:
          de = str(int(a,8));
          print("The integer value is", de);
          break
      

      【讨论】:

      • 解释和答案会很有用。
      • 考虑到))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2015-09-15
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2014-02-13
      相关资源
      最近更新 更多