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
b630514
commit 16e84f8
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/kotlin/malopezrom.kts
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,57 @@ | ||
import kotlin.Number | ||
|
||
/* | ||
* Crea una función que reciba un número decimal y lo trasforme a Octal | ||
* y Hexadecimal. | ||
* - No está permitido usar funciones propias del lenguaje de programación que | ||
* realicen esas operaciones directamente. | ||
*/ | ||
|
||
|
||
|
||
/** | ||
* Funcion principal | ||
*/ | ||
fun main() { | ||
val valor = 255255255 | ||
println("Decimal: $valor") | ||
println("Octal: ${valor.toOctal()}") | ||
println("Hexadecimal: ${valor.toHexadecimal()}") | ||
|
||
} | ||
|
||
/** | ||
* Funcion de extension que convierte un numero decimal a octal | ||
*/ | ||
fun Number.toOctal(): Int { | ||
var octal = 0 | ||
var decimal = this.toInt() | ||
var i = 1 | ||
while (decimal != 0) { | ||
octal += (decimal % 8) * i | ||
decimal /= 8 | ||
i *= 10 | ||
} | ||
return octal | ||
|
||
} | ||
|
||
/** | ||
* Funcion de extension que convierte un numero decimal a hexadecimal | ||
*/ | ||
fun Number.toHexadecimal(): String { | ||
var hexadecimal = "" | ||
var decimal = this.toInt() | ||
while (decimal != 0) { | ||
val value = decimal % 16 | ||
hexadecimal = if (value < 10) { | ||
value.toString() + hexadecimal | ||
} else { | ||
(value + 55).toChar() + hexadecimal | ||
} | ||
decimal /= 16 | ||
} | ||
return hexadecimal | ||
} | ||
|
||
main() |