如何使用Python将八进制转换为十进制 [英] How to use Python to convert an octal to a decimal

查看:1034
本文介绍了如何使用Python将八进制转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Decimal but have no clue how to turn it into code. Any help is appreciated. Thank you.

推荐答案

从十进制到八进制:

oct(42) # '052'

八进制至小数

int('052', 8) # 42

如果要以字符串形式返回八进制,则可能需要将其包装在 str 中。

If you want to return octal as a string then you might want to wrap it in str.

这篇关于如何使用Python将八进制转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆