import struct def hex_to_representations(hex_input): """ Converts a hexadecimal string to its integer, float, and UTF-8 string representations. """ try: # Convert to integer integer_representation = int(hex_input, 16) print(f"In binary: {bin(integer_representation)[2:].zfill(len(hex_input) * 4)}") print(f" Integer representation: {integer_representation}") except ValueError: print(" Cannot convert to integer (invalid hexadecimal for integer).") try: # Convert to float (assuming 32-bit IEEE 754 float) # Remove "0x" prefix if present and ensure even length for bytes.fromhex clean_hex = hex_input.lstrip("0x").zfill(8) # Pad with zeros for 32-bit float (8 hex chars) byte_array = bytes.fromhex(clean_hex) float_representation = struct.unpack('!f', byte_array)[0] print(f" Float representation's byte array (leading: {byte_array}") print(f" Float representation (IEEE 754 32-bit): {float_representation}") except (ValueError, struct.error): print(" Cannot convert to float (invalid hexadecimal for float).") try: # Convert to UTF-8 string # Remove "0x" prefix if present clean_hex = hex_input.lstrip("0x") byte_array = bytes.fromhex(clean_hex) utf8_string_representation = byte_array.decode('utf-8') print(f" UTF-8 string representation: '{utf8_string_representation}'") except (ValueError, UnicodeDecodeError): print(" Cannot convert to UTF-8 string (invalid hexadecimal for UTF-8).") # Get input from the user hex_number = input("Enter a hexadecimal number (e.g., 1A, 22, 48656c6d, 48656c6c6f2c20576f726c6421): ") print(f"Your input hexadecimal number: {hex_number}") # Call the function to display representations hex_to_representations(hex_number)