tags:

views:

51

answers:

3

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.

A: 

You must write a C function to accomplish the conversion. Lua does not have casts or anything like them.

DeadMG
I'm sure you can accomplish the task without adding a function in C to convert it, although I realize this is the easy way out. I'm sure if I understood better how a float data structure worked I could form a string and then convert it to a float in Lua.Since I have the ability to read the memory directly I really don't see what's stopping me from patching together a float.This is a lazy answer I can't vote it down because my rep isn't high enough.
Nowayz
@Nowayz: In theory, that is possible. However, in reality, you'd be making many, many assumptions about the data structure of a float. In addition, you could only ever convert it to a number, which is a double, not a float. In addition, your byte array is not a float. A float is 32bit or 4 byte. You have 12 bytes. That's just one example of how wrong what you're trying to do is. If you had the address in C, then you could just perform a pointer cast to get the raw representation, but it would still be wrong.
DeadMG
+3  A: 

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

lhf
+3  A: 
Norman Ramsey