how to read float bytes from Memory?

Get help with using AutoHotkey and its commands and hotkeys
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

how to read float bytes from Memory?

07 May 2017, 01:00

I am wondering if there is a way to read float bytes from memory directly and put them into an array
I am building some converters for Byte, Nibbles, and Binary for Mini-Framework.
I can work out bytes for any integer type and have also constructed a class to handle UInt64. This is possible as I also built classes for BigInt.
So I have a class for Int16, UInt16, Int32, UInt32, Int64, UInt64, Float and BigInt.

I am working on conversions and parsing but am not sure how to get the bytes for a float value. I know that a float should be 64 bits in AutoHotkey but I am unable to get the bytes to convert to and from the conversion I need.
I thought perhaps NumGet but show more then 64 bytes.

Code: Select all

val := 1.12345675646456456456
MsgBox % VarSetCapacity(val) ; returns 126
Even when I try offseting by 2 bytes and using UShort I am not get correct value.
Any Ideas?

I can Get integer value for float using the following methods

Code: Select all

    _FloatToInt64(input) {
		VarSetCapacity(Var, 8, 0)       ; Variable to hold integer
		NumPut(input, Var, 0, "Double" ) ; Input as Float
		retval := NumGet(Var, 0, "Int64") ; Retrieve it as 'Signed Integer 64'
		VarSetCapacity(Var, 0)
		return retval
	}

	_Int64ToFloat(input) {
		VarSetCapacity(Var, 8, 0)       ; Variable to hold integer
		NumPut(input, Var, 0, "Int64" ) ; Input as Signed Integer 64'
		retval := NumGet(Var, 0, "Double") ; Retrieve it as Float
		VarSetCapacity(Var, 0)
		return retval
	}
For Positive float values i get a positive integer from _Int64ToFloat() and this seems to work as I can convert the Positive integer value to bytes.
However when the float value is negative _Int64ToFloat() returns a negative value and conversion no longer works. I am guessing the bits are being wrap somehow.
For any other integer my conversion work just fine so I know its not my conversion of the integer.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to read float bytes from Memory?

07 May 2017, 08:03

?

Code: Select all

_Int64ToFloat(input) {
    VarSetCapacity(Var, 8, 0)       ; Variable to hold integer
    InStr(input,".")?NumPut(input, Var, 0, "Double" ):NumPut(input, Var, 0, "Int64" ) ; Input as Signed Integer 64' or Float
    retval := NumGet(Var, 0, "Double") ; Retrieve it as Float
    VarSetCapacity(Var, 0)
    return retval
}
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: how to read float bytes from Memory?

07 May 2017, 15:33

I figured it out. It was an issue with the way that I was converting bytes. My negative value integer was not getting complements2 bytes as I thought it was. Once I correct this I was able to get the integer value from _FloatToInt64() and convert into byte array that could be converted back to proper int and then back to float using _Int64ToFloat() when needed.

Thanks
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: how to read float bytes from Memory?

07 May 2017, 15:37

I have just one more question about the _FloatToInt64() and _Int64ToFloat() methods.
What would be the correct size to use for VarSetCapacity() seeing as

Code: Select all

val := 1.12345675646456456456
MsgBox % VarSetCapacity(val) ; returns 126
Settign the Capacity to 8 seems like it might be too small.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to read float bytes from Memory?

07 May 2017, 16:34

Try this, for simplicity double and float will be always 8 byte, though Float is 4 bytes!

Code: Select all

SizeOfInteger(v){
  return InStr(v,".")?8:v>4294967295?8:v>65535?4:v>255?2:v>-129?1:v>-32769?2:v>-2147483649?4:8
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Adamyounguk, Bing [Bot], boiler, bxgchoi, hasantr, mikeyww, Xsawers and 50 guests