PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] How to use Python to convert an octal to a decimal

 August 09, 2022     decimal, octal, python     No comments   

Issue

I had this little homework assignment and I needed to convert decimal to octal and then octal to decimal. I did the first part and could 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 I have no clue how to turn it into code.


Solution

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.



Answered By - martin
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing