Author Topic: how to convert 4 bytes to  (Read 2226 times)

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 908
  • 96A285CC
    • View Profile
    • my youtube channel
how to convert 4 bytes to
« on: 2015-Oct-27 »
a float ?  can anyone help?

MrTAToad

  • Guest
Re: how to convert 4 bytes to
« Reply #1 on: 2015-Oct-28 »
Do the original 4 bytes make up a float ?  Or is it something else ?

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 908
  • 96A285CC
    • View Profile
    • my youtube channel
Re: how to convert 4 bytes to
« Reply #2 on: 2015-Oct-28 »
Yap its a original 32 bit float .

MrTAToad

  • Guest
Re: how to convert 4 bytes to
« Reply #3 on: 2015-Oct-28 »
You should be able to use READSHORTIEEE in that case

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: how to convert 4 bytes to
« Reply #4 on: 2015-Oct-28 »
+1

MrTAToad
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: how to convert 4 bytes to
« Reply #5 on: 2015-Oct-28 »
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]

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 908
  • 96A285CC
    • View Profile
    • my youtube channel
Re: how to convert 4 bytes to
« Reply #6 on: 2015-Oct-28 »
yap union and inline c are the right way.


thx

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2605
    • View Profile
Re: how to convert 4 bytes to
« Reply #7 on: 2015-Oct-28 »
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.
Cheers,

Ian.

“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)

MrTAToad

  • Guest
Re: how to convert 4 bytes to
« Reply #8 on: 2015-Oct-29 »
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.