Created
January 4, 2025 10:07
-
-
Save book000/46b59268e6836baffdb7faa6c4fc1c8e to your computer and use it in GitHub Desktop.
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
| import struct | |
| def float_to_hex(f): | |
| """Convert a float to hex representation in little endian format.""" | |
| return struct.pack('<d', f).hex() | |
| def convert_to_hex(meters): | |
| """Convert meter value to IEEE 754 hex representation.""" | |
| float_hex = float_to_hex(meters) | |
| # Extract the last 8 bytes (16 hex characters) and format the hex string to match the given output format | |
| formatted_hex = ','.join([float_hex[i:i+2] for i in range(0, 16, 2)]) | |
| return f"hex(4):{formatted_hex}" | |
| # Generate the list from 0.0 to 10.0 in 0.1 increments | |
| start = 0.0 | |
| end = 10.0 | |
| step = 0.1 | |
| meter_values = [round(start + step * i, 1) for i in range(int((end - start) / step) + 1)] | |
| # Output the list | |
| for meters in meter_values: | |
| print(f"{meters}m\t{convert_to_hex(meters)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment