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.
Top answer 1 of 4
37
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.
2 of 4
4
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
Django Central
djangocentral.com โบ convert-octal-number-to-decimal-and-vice-versa
Python Program to Convert Octal Number to Decimal and vice-versa
Enter a decimal number :200 The octal equivalent is : 310 ยท An alternate shorthand to convert a decimal number into octal is by using the oct() method.
Videos
Convert Octal to Decimal in Python | Step-by-Step Logic ...
02:28
Python Program to convert octal number to decimal number - YouTube
12:20
Octal To Decimal Conversion In Python With Multiple Ways - YouTube
How to Convert Decimal To Binary, Octal and Hexadecimal ...
Python Program - Convert Decimal to Binary, Octal, and ...
Decimal to Octal Conversion: Easy Guide & Examples! #shorts
Programiz
programiz.com โบ python-programming โบ examples โบ conversion-binary-octal-hexadecimal
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal. Note: To test the program for other decimal numbers, change the value of dec in the program.
PYnative
pynative.com โบ home โบ python โบ programs and examples โบ python convert decimal numbers to octal and vice versa
Python Convert Decimal Number to Octal and Vice Versa โ PYnative
April 22, 2025 - The format() function in Python converts an integer decimal to its octal representation as a string, without the '0o' prefix.
Scaler
scaler.com โบ home โบ topics โบ decimal to octal in python
Decimal to Octal in Python - Scaler Topics
April 13, 2024 - In the above program, we have ... decimal variable becomes zero. The built-in Python method oct() returns the equivalent octal representation of the decimal number passed as a parameter....
YouTube
youtube.com โบ the programming portal
Decimal To Octal Conversion In Python With Multiple Ways - YouTube
In this tutorial you will see how to convert decimal numbers into octal numbers using logical implementation with string. Also, we will use built in methods ...
Published ย August 9, 2023 Views ย 682
Stack Overflow
stackoverflow.com โบ questions โบ 75884669 โบ decimal-to-octal-i-cant-do-this-code-im-just-learning-to-program
python - decimal to octal, I can't do this code, I'm just learning to program - Stack Overflow
Technically, 0 can be converted to octal, though. You have while(l<0) so if your input is greater than 0, it will skip that part entirely. l=int(1/8) means the l will be set to 0 right after.
CodeScracker
codescracker.com โบ python โบ program โบ python-program-convert-decimal-to-octal.htm
Python Program to Convert Decimal to Octal
Python Program to Convert Decimal to Octal - This article is created to cover some programs in Python, to convert decimal number entered by user at run-time to its equivalent octal value. Decimal to Octal using list and while Loop, Using oct() method, function, class
Know Program
knowprogram.com โบ home โบ decimal to octal in python
Decimal to Octal in Python - Know Program
April 2, 2021 - In the previous program, convert decimal to octal using array but in this program, convert decimal to octal without using an array. # Python program to convert decimal to octal def DecimalOctal(num): octal, i = 0, 1 while (num != 0): rem = num % 8 # remainder is calculated octal += rem * i # store exponential value i = i*10 num //= 8 print(octal) # take inputs num = int(input('Enter a decimal number: ')) # calling function and display result print('Octal value: ', end='') DecimalOctal(num)
Tutorials Freak
tutorialsfreak.com โบ python-tutorial โบ examples โบ decimal-to-octal-python
How to Convert Decimal to Octal in Python? Easy Program
Learn how to convert decimal numbers to octal in Python. This tutorial provides practical examples and explanations to enhance your coding skills.
W3Schools
w3schools.com โบ python โบ ref_func_oct.asp
Python oct() Function
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... The oct() function converts an integer into an octal string....
Toppr
toppr.com โบ guides โบ python-guide โบ examples โบ python-examples โบ functions โบ conversion-binary-octal-hexadecimal โบ python-program-to-convert-decimal-to-binary-octal-and-hexadecimal
Python Program to Convert Decimal to Octal, Binary, and Hexadecimal
July 22, 2021 - A. The decimal value of the octal number 177 is 127. ... Q3. What is 10101 as a decimal? A. The decimal equivalent of 10101 is 21. Q4. How do you convert decimal to octal and hexadecimal?
TutorialsPoint
tutorialspoint.com โบ python-program-to-convert-float-decimal-to-octal-number
Python program to convert float decimal to octal number
August 12, 2022 - def float_convert_octal(my_number, places = 3): my_whole, my_dec = str(my_number).split(".") my_whole = int(my_whole) my_dec = int (my_dec) res = oct(my_whole).lstrip("0o") + "." for x in range(places): my_whole, my_dec = str((decimal_converter(my_dec)) * 8).split(".") my_dec = int(my_dec) res += my_whole return res def decimal_converter(num): while num > 1: num /= 10 return num n = input("Enter the floating point value : \n") p = int(input("Enter the number of decimal places of the result : \n")) print(float_convert_octal(n, places = p))
GeeksforGeeks
geeksforgeeks.org โบ dsa โบ program-decimal-octal-conversion
Program for Decimal to Octal Conversion - GeeksforGeeks
If the given decimal number is 16. Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr[0] = 0. Step 2: Divide 16 by 8. New number is 16/8 = 2. Step 3: Remainder, when 2 is divided by 8, is 2. Therefore, arr[1] = 2. Step 4: Divide 2 by 8. New number is 2/8 = 0. Step 5: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore, the equivalent octal number is 20.
Published ย August 5, 2024