Created
January 22, 2026 02:20
-
-
Save kriptobal/8926c5fb1e755f21ad32a37f5c09c6d4 to your computer and use it in GitHub Desktop.
Convertidor de IEEE754 a decimal en python
This file contains hidden or 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
| def ieee754_to_decimal(ieee754): | |
| #Convertimos el decimal a binario de 32 bits | |
| ieee754_binary_string_padded = format(int(ieee754, 16), '0>32b') | |
| #Extraemos el signo | |
| signo = ieee754_binary_string_padded[0] | |
| es_negativo = signo == "1" | |
| #Extraemos el exponente | |
| exponente = ieee754_binary_string_padded[1:9] | |
| #Extraemos la mantisa | |
| mantisa = ieee754_binary_string_padded[9:] | |
| print(f"IEEE 754 Binary: {ieee754_binary_string_padded}") | |
| print(f"Signo: {signo}, Exponente: {exponente}, Mantisa: {mantisa}") | |
| #Primero verificamos que no estemos en presencia del numero 0 | |
| if exponente == "00000000" and mantisa == "00000000000000000000000": | |
| if es_negativo: | |
| print("El numero es -0") | |
| else: | |
| print("El numero es +0") | |
| #Verificamos la mantisa para buscar casos especiales | |
| #Caso 1: infinitos y NaN | |
| if exponente == "11111111": | |
| mantisa_igual_zero = mantisa.find("1") == -1 | |
| if mantisa_igual_zero: | |
| if es_negativo: | |
| print("El numero es infinito negativo") | |
| else: | |
| print("El numero es infinito positivo") | |
| else: | |
| if es_negativo: | |
| print("El numero es NaN") | |
| else: | |
| print("Ha habido un problema el bit de signo deberia ser 0 :(") | |
| return | |
| #Caso 2: subnormales | |
| if exponente == "00000000": | |
| #Calculamos la progresion a lo largo de la mantisa | |
| i = -1 | |
| resultado = 0 | |
| for b in mantisa: | |
| resultado += int(b) * 2 **i | |
| i -= 1 | |
| #Luego ajustamos a exponente subnormal single precision | |
| resultado = resultado * 2**(-126) | |
| if signo == "1": | |
| print(f"El resultado es el subnormal: {-resultado}") | |
| else: | |
| print(f"El resultado es el subnormal: {resultado}") | |
| return | |
| #Caso 3: Numeros normales (el unico caso faltante) | |
| exponente = int(exponente, 2) - 127 | |
| #Calculamos la progresion a lo largo de la mantisa | |
| i = -1 | |
| resultado = 1 | |
| for b in mantisa: | |
| resultado += int(b) * 2 **i | |
| i -= 1 | |
| #Luego ajustamos a exponente normal single precision | |
| resultado = resultado * 2 ** exponente | |
| if signo == "1": | |
| print(f"El resultado es el normal: {-resultado}") | |
| else: | |
| print(f"El resultado es el normal: {resultado}") | |
| return | |
| ieee754 = '0x00000001' | |
| decimal = ieee754_to_decimal(ieee754) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment