The online racing simulator
Seemingly stupid VB question [float to hex]
I've been searching for this on google but I haven't found anything useful. Could be I'm rubbish at searching.

Anyway, I need to convert a float (e.g. 0.35) to a hex value. I then need to split it into bytes but that's not too much of a problem. It seems like there should be an easy solution but I just can't find it

And yes, I am a noob to VB
Not sure about VB.NET but VB6 cannot handle binary or, presumably, hex. So you'd need to code a conversion function or find one off the net.

That said, you're asking about floats and I've no idea how you would display decimal places in hex in the first place.
Basically it's memory values. I thought these were all stored as hex but I'm not so sure anymore. Could anyone spread some light on this?
Quote from Bob Smith :That said, you're asking about floats and I've no idea how you would display decimal places in hex in the first place.

I suppose technically you could say that it's 0.1F (i.e. a 1/16th)... kinda defeats the point of the object though
I've found something and it seems like floats can be written to memory as they are so it solves my problem. Thanks for trying
Quote from Leifde :Basically it's memory values. I thought these were all stored as hex but I'm not so sure anymore. Could anyone spread some light on this?

You can refer to the location of the memory value by hex, but you can't represent all data as hex - living in a base 16 would be make certain things easier, but everything else way too hard.

It's stored as a representation of the corresponding data type - if that makes sense?
I don't think you are supposted to print floats in hex.

In ancient history some computers tryed to use base10 numbers, today every computer uses base2(binary). Never heard base16(hex) computers.

Floats are 'floating point' numbers. I.e the length/accuracy for the integer and decimals parts are not fixed. E.g IEEE754 32bit float uses 1bit sign, 8bit exponent and 23bit significand (iirc).

I don't know what you mean by saving floats "as they are". Floats are just saved in memory as I told in the prev paragraph. You could theoretically save them as a string writing "0.35", but that would be really slow operation to convert them constantly back and forth between float and string.

In old 286-486 days it was sometimes common to 'emulate' decimal numbers with fixed point 16.16 numbers. Basicly they were 32bit integer split into 16bit integer and 16bit decimal parts. It gave some speed advantage in old computers. Definately not recommended to use these nowadays. Just mentioned because these you could print in hex.
Quote from Aquilifer :In ancient history some computers tryed to use base10 numbers, today every computer uses base2(binary). Never heard base16(hex) computers.

base2 and base 16 are pretty much identical


hm working on some tweak app leifde ?
you should look into how those memory manipulation functions actually work
normally if you use one to write a float to some offset the function should do all the encoding work for you assuming the data types match
#9 - Stuff
I'm not sure what you're trying to do by converting floats to hex but I have an idea on how to. Floats, or Single in VB6, is 4 bytes and so is a long. Just CopyMemory the single bytes into a temporary long then convert it to hex with Hex().

Like so..

'declare this somwhere
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

'then do
Call CopyMemory(lngTemp, sngValue, 4)
MsgBox "hex?: " & Hex(lngTemp)

I don't know if its the value you need but I'm assuming bytes are bytes when converted to hex. However, I don't get the same, 32bit answer as PowerToy Calc. On the "good" side, longs and the CopyMemory API is how VB6 handles things like pointers and memory manipulation.

Hope it helps?
It's more of a mechanik program than tweak Shotglass

I was being a bit thick last night. I get how it works now, it just took a while to get through to me

Thanks for the help everyone
I'm confused again. I'm getting this error message:
Quote :A call to PInvoke function 'WindowsApplication1!WindowsApplication1.Memory::WriteProcessMemory' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

I've searched on google but all I couldn't find any useful answers, only people telling the OPs to use a different method. AFAIK there isn't a different method.

My module
Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Integer, ByVal lpBuffer As Double, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long

Sub WriteDouble(ByVal proc As Long, ByVal address As Integer, ByVal value As Double, ByVal length As Integer)
If (WriteProcessMemory(proc, address, value, length, 0)) Then
MsgBox("Worked!")
Else
MsgBox("FAILED!")
End If
End Sub

Sub from my form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (SetProcess("LFS_W")) Then
WriteDouble(mGameProcess.Handle, &H57D380, 0.35, 4)
Else
MsgBox("Failed!")
End If
End Sub

Any ideas?

EDIT: I'm using .net (AFAIK. I'm using VB 2005 for my compiler)
I would bet that you write something to the wrong location in the memory.

EDIT: I was wrong, the 57D380 is the address, am I right? Then I would check, that the value is correct and doesn't point somewhere, e.g. to the stack.
Hmm, that address is correct, it works when I use that address in Artmoney. I don't quite get what you mean but that particular address is for a 4-byte float, which, AFAIK, is what I'm writing to it
Ok, If you are able to write some 4-bytes value to that address in Artmoney, then it should be correct.

What about to change "ByVal lpBuffer As Double" to "ByRef lpBuffer As Double" in the WriteProcessMemory declaration?
LFSTweak works with just Singles, no Doubles.... Maybe that is your issue?
Just singles? I thought general power was usually 0.19, which is AFAIK a double?

Anyway, I'll have a look tomorrow since it's about 1:45am
Im not using a module, just some delcarations at the top, and a couple of subs:


Declarations:

Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
Private Declare Function ReadProcessMem Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

SUBS:
Sub WriteAInt(Address As Long, Value As Integer)

Dim hwnd As Long, pid As Long, pHandle As Long

hwnd = FindWindow(vbNullString, "Live For Speed")
If (hwnd <> 0) Then
GetWindowThreadProcessId hwnd, pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
If (pHandle <> 0) Then
WriteProcessMemory pHandle, Address, Value, 1, 0&
End If
CloseHandle pHandle
End If


End Sub

Sub WriteAIntSINGLE(Address As Long, Value As Single)

Dim hwnd As Long, pid As Long, pHandle As Long

hwnd = FindWindow(vbNullString, "Live For Speed")
If (hwnd <> 0) Then
GetWindowThreadProcessId hwnd, pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
If (pHandle <> 0) Then
WriteProcessMemory pHandle, Address, Value, 4, 0&
End If
CloseHandle pHandle
End If



End Sub

then all I do is call the sub when I need something written:


Call WriteAIntSINGLE(GTRimWidthRearAddress, 0.1905)
Call WriteAInt(CarType1MinCylAddress, &H1)

(Really it doesnt matter if I push hex, or an integer, they both work)

BTW, I havent worked with my code in a LOOOONG time, and am stale....
However it appears my WriteAIntSINGLE sub writes 4 bytes, as my WriteAInt only writes 1
redrum now that youre here how do you check if lfs is unlocked ?
I've moved to VB6 now. It's working fine but valid hotlaps can be made with a V10 BF1

I just got the WR on the oval by 0.7s (I deleted it straight away). Any idea how I make hotlaps not valid using it?

FGED GREDG RDFGDR GSFDG