[Python] Convert a Python float value to Turbo Pascal's 48‑bit real in Python

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to convert a floating number to 48‑bit Real (from Borland Pascal) in Python. I have found this method written in C# (I think):

    static byte[] DoubleToReal48(double d)
    {
    byte[] r = new byte[6];

    long bits = BitConverter.DoubleToInt64Bits(d);
    bool negative = ((bits >> 63) & 1) != 0;
    long exponent = ((bits >> 52) & 0x7FF) - 1023;
    long mantissa = bits & 0xFFFFFFFFFFFFFL;

    long raw = (negative ? 1 : 0);
    raw = (raw << 39) | (mantissa >> 13);
    raw = (raw << 8) | ((exponent + 129) & 0xFF);

    for (int k = 0; k < 6; k++)
    {
    r[k] = (byte)(raw & 0xFF);
    raw >>= 8;
    }
    return r;
    }


    I don’t know C#, so I’m having a hard time translating it to Python. Can anyone help me? Or perhaps there is a better way?

    I have found this but it seems to only convert 48‑bit Real to double, not the other way around as I need.

    Continue reading...

Compartilhe esta Página