Byte to Float

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

cornfedLynn

New Member
Oct 10, 2003
23
0
0
Has anyone seen a way to construct a float out of bytes?
I've gone down many "casting" paths and just end up with a big integer.

Going down the "array" path, I've gotten the following to compile, but the game crashes when the code setting the variable is called:
.
.
.
local byte B[255];
local array<float> myTestArray;
.
.
B[0]=0;
B[1]=128;
B[2]=173;
B[3]=67;
.
.

myTestArray[0] = float(B);

Thanks for any feedback.
 

jimboh

Salty Sea Dog
Jan 20, 2004
140
0
0
Roaming the high seas...
You're trying to typecast the entire array of B to a single float. As far as I know...that's impossible...

You might want to try using a for loop:
Code:
local byte B[255];
local array<float> myTestArray;
local int i;

// Your code...

for (i=0; i<255; i++)
  myTestArray[i] = float(B[i]);

-Jimboh
 

cornfedLynn

New Member
Oct 10, 2003
23
0
0
I think that you're right - the typecast is impossible.

I wrote my own conversion function using the IEEE spec. Depending on Endian'ness, the order the bytes are sent to the function may have to be changed, but this worked for my purposes:

function float byteToFloat(byte floatWord[4])
{
local int i;
local float sign;
local float exponent;
local int fractionHold;
local float fraction;
local byte binaryFloat[32];
local float convertedFloat;

for(i=0;i<4;i++)
{
if (floatWord >= 128)
{
binaryFloat[(7+(8*i))]=1;
floatWord=floatWord-128;
}

if (floatWord >= 64)
{
binaryFloat[(6+(8*i))]=1;
floatWord=floatWord-64;
}

if (floatWord >= 32)
{
binaryFloat[(5+(8*i))]=1;
floatWord=floatWord-32;
}

if (floatWord >= 16)
{
binaryFloat[(4+(8*i))] = 1;
floatWord=floatWord-16;
}

if (floatWord >= 8)
{
binaryFloat[(3+(8*i))] = 1;
floatWord=floatWord-8;
}

if (floatWord >= 4)
{
binaryFloat[(2+(8*i))]=1;
floatWord=floatWord-4;
}

if (floatWord >= 2)
{
binaryFloat[(1+(8*i))]=1;
floatWord=floatWord-2;
}

if (floatWord >= 1)
{
binaryFloat[(0+(8*i))]=1;
floatWord=floatWord-1;
}


}

for(i=0;i<=22;i++)
{
if(binaryFloat==1)
{
fractionHold=fractionHold+(2**i);
}

}

fraction=float(string(1)$"."$string(fractionHold));

for(i=23;i<=30;i++)
{
if(binaryFloat==1)
{
exponent=exponent+(2**(i-23));
}
}

exponent=exponent-127;

if (binaryFloat[31]==1)
{
sign=-1;
}
if (binaryFloat[31]==0)
{
sign=1;
}

convertedFloat=sign*(2**exponent)*fraction;


return convertedFloat;

}

jimboh said:
You're trying to typecast the entire array of B to a single float. As far as I know...that's impossible...

You might want to try using a for loop:
Code:
local byte B[255];
local array<float> myTestArray;
local int i;

// Your code...

for (i=0; i<255; i++)
  myTestArray[i] = float(B[i]);

-Jimboh