of     1   

ForeverDev
#181537412Tuesday, January 12, 2016 3:52 AM GMT

i need to convert a decimal to a hexadecimal string i've tried string.format("%x", decimal) and tostring(tonumber(decimal, 16)) but they only work for integers (e.g. if I try to get the hexa of 10.5, it will truncate 10.5 into 10 and return 'a') any ideas?
ForeverDev
#181537592Tuesday, January 12, 2016 3:55 AM GMT

basically i need a floating point representation of any given number in base 16 :/
LegendaryAccount
#181537787Tuesday, January 12, 2016 3:58 AM GMT

Found this on the internet. function float2hex (n) if n == 0.0 then return 0.0 end local sign = 0 if n < 0.0 then sign = 0x80 n = -n end local mant, expo = math.frexp(n) local hext = {} if mant ~= mant then hext[#hext+1] = string.char(0xFF, 0x88, 0x00, 0x00) elseif mant == math.huge or expo > 0x80 then if sign == 0 then hext[#hext+1] = string.char(0x7F, 0x80, 0x00, 0x00) else hext[#hext+1] = string.char(0xFF, 0x80, 0x00, 0x00) end elseif (mant == 0.0 and expo == 0) or expo < -0x7E then hext[#hext+1] = string.char(sign, 0x00, 0x00, 0x00) else expo = expo + 0x7E mant = (mant * 2.0 - 1.0) * math.ldexp(0.5, 24) hext[#hext+1] = string.char(sign + math.floor(expo / 0x2), (expo % 0x2) * 0x80 + math.floor(mant / 0x10000), math.floor(mant / 0x100) % 0x100, mant % 0x100) end return tonumber(string.gsub(table.concat(hext),"(.)", function (c) return string.format("%02X%s",string.byte(c),"") end), 16) end function hex2float (c) if c == 0 then return 0.0 end local c = string.gsub(string.format("%X", c),"(..)",function (x) return string.char(tonumber(x, 16)) end) local b1,b2,b3,b4 = string.byte(c, 1, 4) local sign = b1 > 0x7F local expo = (b1 % 0x80) * 0x2 + math.floor(b2 / 0x80) local mant = ((b2 % 0x80) * 0x100 + b3) * 0x100 + b4 if sign then sign = -1 else sign = 1 end local n if mant == 0 and expo == 0 then n = sign * 0.0 elseif expo == 0xFF then if mant == 0 then n = sign * math.huge else n = 0.0/0.0 end else n = sign * math.ldexp(1.0 + mant / 0x800000, expo - 0x7F) end return n end
ForeverDev
#181537858Tuesday, January 12, 2016 3:59 AM GMT

" if mant == 0 then n = sign * math.huge else n = 0.0/0.0 end " oh my god what is this hackyness
LegendaryAccount
#181538017Tuesday, January 12, 2016 4:02 AM GMT

Not sure if this is useful or better. http://lua-users.org/lists/lua-l/2011-12/msg00035.html
ForeverDev
#181538088Tuesday, January 12, 2016 4:03 AM GMT

the first one didnt work the second one i looked at earlier and its not in the format i need :(
cntkillme
#181538405Tuesday, January 12, 2016 4:07 AM GMT

Do you need a decimal to float or decimal to double? Regardless, Lua's math.frexp will give you the significant (far less tedious than doing it manually). Here's one I made (decimal -> byte array) lib.float64ToBytes = function(self, double, littleEndian) local bytes = {}; local sign = double < 0 and 128 or 0; local mantissa, exponent; double = math.abs(double); mantissa, exponent = math.frexp(double); exponent = exponent + 1022; mantissa = math.floor(mantissa * (2 ^ 53)); bytes[8] = sign + self:shiftRight(exponent, 4); bytes[7] = self:shiftLeft(self:getBit(exponent, 0, 4), 4) + self:getBit(mantissa, 48, 4); for key = 6, 1, -1 do bytes[key] = self:getBit(mantissa, 8 * (key - 1), 8); end if not littleEndian then self:flipArray(bytes); end return bytes; end; It actually is not entirely correct (does not handle infinities and NaNs) but that's easy to implement (which I have in some other script that I can't find).
cntkillme
#181538433Tuesday, January 12, 2016 4:08 AM GMT

significand*
ForeverDev
#181540420Tuesday, January 12, 2016 4:43 AM GMT

decimal to double is what i need i'll take a look at that
cntkillme
#181540457Tuesday, January 12, 2016 4:44 AM GMT

It relies on other functions that you can easily recreate. If not I'll give those to you too.

    of     1