简体   繁体   English

Python 2.5:如何将float转换为hex?

[英]python 2.5: how to convert float to hex?

I want to convert a float (the output of time.time() ) in hex using python 2.4/2.5. 我想使用python 2.4 / 2.5将十六进制转换为浮点数( time.time()的输出time.time()

I found tons of examples that convert hex to float, but I cannot find anything that allows me to do what the float.hex() of python >= 2.6 does. 我发现了很多将十六进制转换为float的示例,但是找不到任何可以执行python> = 2.6的float.hex()的事情。

It can't be done portably in Python before version 2.6; 不能在2.6版之前的Python中移植; it needs the information in sys.float_info, which is new in Python 2.6. 它需要sys.float_info中的信息,这是Python 2.6中的新增功能。

If you want to do it non-portably, ie for your particular platform, you would need to look at the float.h file for the C compiler that was used to produce your 2.4/5 Python, or at the sys.float_info returned by a 2.6 or 2.7 implementation on your platform (and trust that it applies to your 2.4/5 Python). 如果要非便携式地执行此操作,即针对您的特定平台,则需要查看用于生成2.4 / 5 Python的C编译器的float.h文件,或者查看由以下代码返回的sys.float_info您平台上的2.6或2.7实现(并相信它适用于您的2.4 / 5 Python)。 Then you would need to look at the float_hex function in the Python source (Objects/floatobject.c) and translate that to Python and test it (against a 2.6/7 Python, perhaps). 然后,您需要查看Python源代码(Objects / floatobject.c)中的float_hex函数,并将其转换为Python并进行测试(也许是针对2.6 / 7 Python)。

This seems like a lot of work, for what? 这似乎是一项艰巨的工作,是为了什么? What is your goal? 你的目标是什么? What do you want to do that can't be achieved with repr(your_float) ? 您想做什么是repr(your_float)无法实现的?

Edit : need for a unique identifier 编辑 :需要一个唯一的标识符

Note that time.time() is not very precise: 请注意,time.time()不太精确:

""" time.time() Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls. """ “”“ time.time()以UTC时间返回以纪元为单位的时间(以秒为单位)表示的时间。请注意,即使始终将时间返回为浮点数,也不是所有系统都可以提供精度更高的时间小于1秒。虽然此函数通常返回非递减值,但如果两次调用之间的系统时钟已被调回,则它可以返回比上一个调用更低的值。

Allowing for up to a billionth of a second resolution: 允许高达十亿分之一秒的分辨率:

>>> hex(int(time.time() * 1000000000))
'0x11ef11c41cf98b00L'
>>>

Is that good enough? 这样够好吗?

Here is the C version of the code, I don't have time to port it right now, but maybe someone else can. 这是代码的C版本,我现在没有时间移植它,但是也许其他人可以。

float_hex(PyObject *v)
{
    double x, m;
    int e, shift, i, si, esign;
    /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
       trailing NUL byte. */
    char s[(TOHEX_NBITS-1)/4+3];

    CONVERT_TO_DOUBLE(v, x);

    if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
        return float_str((PyFloatObject *)v);

    if (x == 0.0) {
        if (copysign(1.0, x) == -1.0)
            return PyString_FromString("-0x0.0p+0");
        else
            return PyString_FromString("0x0.0p+0");
    }

    m = frexp(fabs(x), &e);
    shift = 1 - MAX(DBL_MIN_EXP - e, 0);
    m = ldexp(m, shift);
    e -= shift;

    si = 0;
    s[si] = char_from_hex((int)m);
    si++;
    m -= (int)m;
    s[si] = '.';
    si++;
    for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
        m *= 16.0;
        s[si] = char_from_hex((int)m);
        si++;
        m -= (int)m;
    }
    s[si] = '\0';

    if (e < 0) {
        esign = (int)'-';
        e = -e;
    }
    else
        esign = (int)'+';

    if (x < 0.0)
        return PyString_FromFormat("-0x%sp%c%d", s, esign, e);
    else
        return PyString_FromFormat("0x%sp%c%d", s, esign, e);
}

Of course it can be done in a portable way, it's just maths. 当然,这可以通过便携式方式完成,这只是数学。 Here's how (inclusing tests proving it works). 这是这样的(包括测试证明它是有效的)。

from __future__ import division

MAXHEXADECIMALS = 10

def float2hex(f):
    w = f // 1
    d = f % 1

    # Do the whole:
    if w == 0:
        result = '0'
    else:
        result = ''
    while w:
        w, r = divmod(w, 16)
        r = int(r)
        if r > 9:
            r = chr(r+55)
        else:
            r = str(r)
        result =  r + result

    # And now the part:
    if d == 0:
        return result

    result += '.'
    count = 0
    while d:
        d = d * 16
        w, d = divmod(d, 1)
        w = int(w)
        if w > 9:
            w = chr(w+55)
        else:
            w = str(w)
        result +=  w
        count += 1
        if count > MAXHEXADECIMALS:
            break

    return result


import unittest
class Float2HexTest(unittest.TestCase):

    def test_ints(self):
        assert float2hex(0x25) == '25'
        assert float2hex(0xFE) == 'FE'
        assert float2hex(0x00) == '0'
        assert float2hex(0x01) == '1'
        assert float2hex(0x14E7F400A5) == '14E7F400A5'

    def test_floats(self):
        assert float2hex(1/2) == '0.8'
        assert float2hex(1/13) == '0.13B13B13B13'
        assert float2hex(1034.03125) == '40A.08'

suite = unittest.makeSuite(Float2HexTest)
runner = unittest.TextTestRunner()
runner.run(suite)

Yeah, that's pretty pointless. 是的,那毫无意义。 :-) Of course, the correct answer in this case is to not convert a float to hex, but to use an INTEGER representation of time and convert that to a hex string. :-)当然,在这种情况下,正确的答案是不将浮点数转换为十六进制,而是使用时间的INTEGER表示并将其转换为十六进制字符串。 But still, it can be done. 但是仍然可以做到。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM