forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c927fc
commit 6800814
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/python/TiagoAlvarezSchiaffino.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
def convert_to_octal_hexadecimal(decimal_number): | ||
""" | ||
Convert a decimal number to Octal and Hexadecimal. | ||
Args: | ||
decimal_number (int): The decimal number to convert. | ||
Returns: | ||
tuple: A tuple containing the Octal and Hexadecimal representations. | ||
""" | ||
octal_result, hexadecimal_result = convert_to_base(decimal_number, 8), convert_to_base(decimal_number, 16) | ||
return octal_result, hexadecimal_result | ||
|
||
def convert_to_base(decimal, base): | ||
""" | ||
Convert a decimal number to the specified base. | ||
Args: | ||
decimal (int): The decimal number to convert. | ||
base (int): The base to convert to. | ||
Returns: | ||
str: The converted number in the specified base. | ||
""" | ||
digits = "0123456789ABCDEF" | ||
result = [] | ||
|
||
while decimal >= base: | ||
result.append(digits[decimal % base]) | ||
decimal //= base | ||
|
||
result.append(digits[decimal]) | ||
reversed_result = result[::-1] | ||
|
||
return "".join(reversed_result) | ||
|
||
decimal_number = 255 | ||
octal, hexadecimal = convert_to_octal_hexadecimal(decimal_number) | ||
print(f"The decimal number {decimal_number} converts to octal as {octal}") | ||
print(f"The decimal number {decimal_number} converts to hexadecimal as {hexadecimal}") |