52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
"""
|
|
Clase híbrida para números hexadecimales
|
|
"""
|
|
from hybrid_base import HybridCalcType
|
|
|
|
|
|
class HybridHex(HybridCalcType):
|
|
"""Clase híbrida para números hexadecimales"""
|
|
|
|
def __new__(cls, value_input):
|
|
"""Crear objeto SymPy válido"""
|
|
obj = HybridCalcType.__new__(cls)
|
|
return obj
|
|
|
|
def __init__(self, value_input):
|
|
"""Inicialización de Hex"""
|
|
# Convertir input a entero
|
|
if isinstance(value_input, str):
|
|
# Remover prefijo 0x si existe
|
|
if value_input.startswith('0x'):
|
|
value_input = value_input[2:]
|
|
# Convertir a entero
|
|
value = int(value_input, 16)
|
|
else:
|
|
value = int(value_input)
|
|
|
|
# Llamar al constructor base
|
|
super().__init__(value, f"0x{value:02x}")
|
|
|
|
def __str__(self):
|
|
"""Representación string para display"""
|
|
return f"0x{self._value:02x}"
|
|
|
|
def _sympystr(self, printer):
|
|
"""Representación SymPy string"""
|
|
return str(self)
|
|
|
|
@staticmethod
|
|
def Helper(input_str):
|
|
"""Ayuda contextual para Hex"""
|
|
return """
|
|
Formato Hex:
|
|
- Con prefijo: 0x1A
|
|
- Sin prefijo: 1A
|
|
|
|
Conversiones:
|
|
- toDecimal(): Convierte a decimal
|
|
"""
|
|
|
|
def toDecimal(self):
|
|
"""Convierte a decimal"""
|
|
return self._value |