floating-pointluamemcpy

Convert byte array to float in Lua [Float Data Structure]


I have the ability to read the memory with Lua, but I've run into a problem. I want to read a float value.

I have, what I presume to be a float, in the memory which looks like this.

Byte Array 65 B6 40 43 EC 35 87 41 51 04 9E 3F
Float Value 192.712478637695

I know the float value because I'm using a memory editor. In Lua I have a function similar to memcpy, it's called readmem(Address,bytes) it returns an integer value.

How would I read the byte array into a float in Lua using readmem.

For the sake of this question I suppose you could assume that the 6 in 65 is address 00000000.


Solution

  • Weird, this seems to be stored in middle-endian order. (I checked this with http://www.h-schmidt.net/FloatApplet/IEEE754.html)

    Anyway, you can do the conversion in pure Lua using code from http://yueliang.luaforge.net/

    See also http://lua-users.org/lists/lua-l/2010-03/msg00910.html

    If you can use C, try one of the libraries listed in http://lua-users.org/wiki/StructurePacking