{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import struct\n", "import binascii" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "f_max_s = '7fefffffffffffff'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x7f\\xef\\xff\\xff\\xff\\xff\\xff\\xff'\n" ] } ], "source": [ "print(binascii.unhexlify(f_max_s))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(binascii.unhexlify(f_max_s)))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1.7976931348623157e+308,)\n" ] } ], "source": [ "print(struct.unpack('>d', binascii.unhexlify(f_max_s)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7976931348623157e+308\n" ] } ], "source": [ "print(struct.unpack('>d', binascii.unhexlify(f_max_s))[0])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(struct.unpack('>d', binascii.unhexlify(f_max_s))[0]))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def hex_to_double(s):\n", " if s.startswith('0x'):\n", " s = s[2:]\n", " s = s.replace(' ', '')\n", " return struct.unpack('>d', binascii.unhexlify(s))[0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7976931348623157e+308\n" ] } ], "source": [ "print(hex_to_double('7fefffffffffffff'))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7976931348623157e+308\n" ] } ], "source": [ "print(hex_to_double('0x7fefffffffffffff'))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.7976931348623157e+308\n" ] } ], "source": [ "print(hex_to_double('0x7fef ffff ffff ffff'))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.195\n" ] } ], "source": [ "print(hex_to_double('0x4045 18f5 c28f 5c29'))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "inf\n" ] } ], "source": [ "print(hex_to_double('7ff0000000000000'))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nan\n" ] } ], "source": [ "print(hex_to_double('7ff0000000000001'))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5e-324\n" ] } ], "source": [ "print(hex_to_double('0000000000000001'))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "# print(hex_to_double('ffff ffff ffff ffff ff'))\n", "# error: unpack requires a buffer of 8 bytes" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# print(hex_to_double('ffff ffff ffff ff'))\n", "# error: unpack requires a buffer of 8 bytes" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def hex_to_float(s):\n", " if s.startswith('0x'):\n", " s = s[2:]\n", " s = s.replace(' ', '')\n", " return struct.unpack('>f', binascii.unhexlify(s))[0]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.19499969482422\n" ] } ], "source": [ "print(hex_to_float('0x4228c7ae'))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }