如何使用 Python 将八进制转换为十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35450560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Python to convert an octal to a decimal
提问by Austin Wildgrube
I have this little homework assignment and I needed to convert decimal to octal and then octal to decimal. I did the first part and can not figure out the second to save my life. The first part went like this:
我有这个小小的家庭作业,我需要将十进制转换为八进制,然后将八进制转换为十进制。我做了第一部分,无法弄清楚第二部分来挽救我的生命。第一部分是这样的:
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)
I read how to convert it here: Octal to Decimalbut have no clue how to turn it into code. Any help is appreciated. Thank you.
我在这里阅读了如何转换它:八进制到十进制,但不知道如何将其转换为代码。任何帮助表示赞赏。谢谢你。
采纳答案by martin
From decimal to octal:
从十进制到八进制:
oct(42) # '052'
Octal to decimal
八进制转十进制
int('052', 8) # 42
If you want to return octal as a string then you might want to wrap it in str
.
如果要将八进制作为字符串返回,则可能需要将其包装在str
.
回答by martin
Someone might find these useful
有人可能会发现这些有用
These first lines take any decimal number and convert it to any desired number base
这些第一行采用任何十进制数并将其转换为任何所需的数字基数
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)
The second lines do the same but for a given range and a given decimal
第二行做同样的事情,但对于给定的范围和给定的小数
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)
The third lines convert from any base back to decimal
第三行从任何基数转换回十进制
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)
Note: I also have the GUI version if anyone needs them
注意:如果有人需要,我也有 GUI 版本
回答by Edi Gogidze
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))
回答by Edi Gogidze
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))
回答by Jagath Gowda
I am not an expert in Python.. but I wrote this logic.. it works fine. Feel free to suggest to me if this is not the right way to do it.
我不是 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)