how to convert 4 bytes to

Previous topic - Next topic


a float ?  can anyone help?


Do the original 4 bytes make up a float ?  Or is it something else ?


Yap its a original 32 bit float .


You should be able to use READSHORTIEEE in that case


I <3 DGArray's :D

AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard


If you're not reading this from a file, can you use an INLINE C++ Union? (Map the four bytes to a float).
I haven't programmed in C++ in 10+ years, no idea if this is an option, or even what you're looking for.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]


yap union and inline c are the right way.



Not my code but here it is in C, two forms, one for each endianess.

Code (glbasic) Select
float bytesToFloatA(uchar b0, uchar b1, uchar b2, uchar b3)
    float output;

    *((uchar*)(&output) + 3) = b0;
    *((uchar*)(&output) + 2) = b1;
    *((uchar*)(&output) + 1) = b2;
    *((uchar*)(&output) + 0) = b3;

    return output;

float bytesToFloatB(uchar b0, uchar b1, uchar b2, uchar b3)
    float output;

    *((uchar*)(&output) + 3) = b3;
    *((uchar*)(&output) + 2) = b2;
    *((uchar*)(&output) + 1) = b1;
    *((uchar*)(&output) + 0) = b0;

    return output;

float (*correctFunction)(uchar b0, uchar b1, uchar b2, uchar b3) = bytesToFloatA;

if ((*correctFunction)(0x3e, 0xaa, 0xaa, 0xab) != 1.f/3.f) // horrifying, I know
  correctFunction = bytesToFloatB;

Should be quite easy to inline.


"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)


You just have to make sure the order is correct, otherwise might get the wrong value.  You could also use memcpy if you know if the endianess doesn't change.