[Closed] 16bit float
I found some information on implementing 16bit float to 32bit float conversion or plain reading of 16bit floats in max but i am not sure how to do this in max.
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23967775.html
http://en.wikipedia.org/wiki/Floating_point
Thanks for any help in advance
this is the code to convert it in c#
UInt16 mantissa = (UInt16)(value & 0x3FF); (extracts the last 10 bits)
Byte exponent = (Byte)((value >> 10) & 0x1F); (extracts bits 2 to 6)
Byte sign = (Byte)(value >> 15); (extracts first bit)
can anyone convert this to max script
here’s the script for extracting the sign, exponent and fraction from a 32 bit float in maxscript. dunno how to get the mantisa though.
/*
CG_T
reference http://sandbox.mc.edu/~bennet/cs110/flt/ftod.html
--charles bary
*/
(
fl = 6.5
fli = bit.floatasint fl
sign = bit.get fli 32
-- bit.hexasint "7f800000" = 2139095040;
exponent = bit.shift (bit.and fli 2139095040) -23
-- bit.hexasint "7fffff" = 8388607
fraction = (bit.and fli 8388607)
--fraction is displayed as hex, user prefrence really
format "sign = %, exponent = %, fraction (in hex) = %
" (sign) (exponent-127) (bit.intashex fraction)
)
Edit*: here’s the code for reading a 16 bit integer from a binary file and converting it to a 32 bit float
(
fn convertTo32 input16 =
(
inputAsInt = input16
sign = bit.get inputAsInt 16
exponent = (bit.shift (bit.and inputAsInt (bit.hexasint "7C00")) -10) as integer - 16
fraction = bit.and inputAsInt (bit.hexasint "03FF")
if sign==true then sign = 1 else sign = 0
exponentF = exponent + 127
--Ouput 32 bit integer representing a 32 bit float
outputAsFloat = bit.or (bit.or (bit.shift fraction 13) (bit.shift exponentF 23)) (bit.shift sign 31)
--Output Check
return bit.intasfloat outputasfloat
)
filename = createfile "D:\\myBin.bin"
filename = fopen "D:\\myBin.bin" "wb"
writeshort filename 18080 --16bit float representation of 3.3125
fclose filename
filename = fopen "D:\\myBin.bin" "rb"
input16 = readshort filename
fclose filename
convertTo32 input16
)
cheers!