Hi,
silly issue : I would like to convert a float to hex (8 digits).
“Hex” seems not work with float number.
Have you got an idea ?
Thanks a lot
Hi,
silly issue : I would like to convert a float to hex (8 digits).
“Hex” seems not work with float number.
Have you got an idea ?
Thanks a lot
[quote=172011:@Dominik Fusina]Hi,
silly issue : I would like to convert a float to hex (8 digits).
“Hex” seems not work with float number.
Have you got an idea ?
Thanks a lot[/quote]
Hex is usually used for integers. But you can probably do this :
dim x as double = microseconds/1000000
dim myhex as string = hex(floor(x))+"."+hex((x-floor(x))*1000000)
msgbox myhex
It will separate the integer from the mantissa, and represent each as Hex, separated by the dot.
It’s a kludge, just off the top of my head.
[code]Dim My Float As Single
Dim Msg As String
Dim m as MemoryBlock
m = New MemoryBlock(4)
m.LittleEndian = False
MyFloat = 2.14567
m.SingleValue(0) = MyFloat
For i = 0 To 3
Msg = Msg + Right(“00” + Hex(m.Byte(i)), 2)
Next
[/code]
Msg is your String that represents the hexadecimal value. It may have leading zero’s which isn’t usual rendering but you can add code for that.
or use a memoryblock… but that doesn’t mean the result would mean anything to any other environment
[/code][quote=172011:@Dominik Fusina]I would like to convert a float to hex (8 digits).[/quote]
[code]
Dim f As Double = 1234.34563
System.DebugLog Str(f, “0.00000”) // check the precision
Dim m As New MemoryBlock(4) // for 8 hex digits, you must pack the value in 4 bytes, so it must be Single, Doubles uses 8 bytes
m.SingleValue(0) = f // Converting to 4 bytes loses precision or causes overflow errors (value does not fit)
Dim theHex As String = EncodeHex(Chrb(m.byte(0))+Chrb(m.byte(1))+Chrb(m.byte(2))+Chrb(m.byte(3))) // here is the hex
System.DebugLog theHex
// Lets reverse, decode each hex byte and put it back in m(i) {the 0,1,2,3 bytes reconstructing the Single}, then use it.
// Well, the old value stays there let’s just use it for this this example
f = m.SingleValue(0)
System.DebugLog Str(f, “0.00000”)
// See… It lost precision. 1234.34563 -> 1234.34558
Would be great being possible to fix the mistypes due to the quote in the wrong place.
The first question to Dominik should be:
Why? Who will read it? What floating point format do you want to encode?
Thank you everybody for your precious help
[quote=172034:@Christian Schmitz]The first question to Dominik should be:
Why? Who will read it? What floating point format do you want to encode?[/quote]
Christian, I need to encode several float values into data files in hexadecimal format.
use SQLite database for your data files and you have less worries.
and a correct solution could be:
[code] dim d as Double = 1.23
// encode
dim m as new MemoryBlock(8)
m.LittleEndian = true
m.DoubleValue(0) = d
dim h as string = EncodeHex(m)
// decode
dim mm as MemoryBlock = DecodeHex(h)
mm.LittleEndian = true
dim dd as Double = mm.DoubleValue(0)
Break[/code]
EncodeHex is better than Hex as it can encode a chunk of bytes.
I understand. But my project is a little bit more complex : Each file must have to contain configuration data, like “profiles”.
In my case, it’s easier to update one or two files than a “big” SQLite Database. It’s more safe too
Consider using JSON for the files. It’s a simple format, you can organize it how you wish, and you can store the values directly without encoding.
I would say it’s easier to send UPDATE SQL command to a SQLite database than reading, parsing, changing and writing a binary file.