I want to convert for example 85.0 converts to Ox42aa
Do you know somebody who knows somebody who might be able to explain this answer to me?
Is it a two byte representation of the floating point numbers?
I had use the 4 byte (single precision) numbers for the ModBus communication, and a special 3 byte number representation one for the Honeywell DE (Digital Enchanced) protocoll. And of course I know the 8 byte (double precision) version, what the LO Calc uses.
.
But i never heard about the 2 byte representation of the floating point numbers. Where and how you want to use it?
That example is incomplete and just the upper two bytes of a 32-bit float where 85 decimal is represented as 0x42AA0000 (binary 0 10000101 01010100000000000000000).
Here is a sample file embedded some StarBasic functions to convert Single and Double precision numbers to Hexadecimal strings.
The macros use the Pipe technology.
Pipe2.ods (18.5 KB)
(Fixed - I hope it)
Here is a sample file
Just needs to replace ClosePipe
with CloseMyPipe
in one place (in PipeSingle2HexStr
).
A possible problem could be handling of endianness on some (less common) platforms. But hopefully, the XData*Stream
will handle that cross-platform.
Sorry, I just tried to copy the macros from the MyMacros, but with different name… (The “original name” was ClosePipe in the MyMacros)
I need to do the conversion within the spreadsheet
Please do not use the Answer or Suggest a solution field for comments that are not an answer to the original question / solution to the problem, use Comment instead. Thanks.
The conversion itself is fairly simple in Python:
def float_to_hex(f):
return hex(struct.unpack('<I', struct.pack('<f', f))[0])
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
(taken from python - How to convert a float into hex - Stack Overflow).
You’d need to either create a Calc Add-In extension of that, or glue Python calls into a BASIC user defined function (UDF) callable from Calc. You might get some ideas from Basic to Python.
See also Macros/Python Guide/Calling Python from Basic - The Document Foundation Wiki.
Here is an extension built using this method:
pyFloatToHex.oxt.ods (2.6 KB) (updated to include the improvements from @karolus)
Remove the “ODS” extension to have pyFloatToHex.oxt
, and use FLOATTOIEEE754HEX
and DOUBLETOIEEE754HEX
.
This gives the same results as the nice macro by @Zizi64 in #13. The advantage of the extension is much better performance (about 100 times faster on my system: 1 000 000 random numbers take ~1000 s with the macro, ~11 s with the extension). The advantage of the macro is its self-containment (the file with the macro is all that you need, no matter on which system you open it).
Please make sure you compare comparable things - i.e., please define the padding and width
I can understand most of the files in your extension. But i have not any clue how to create/generate a XPyFloatToHex.rdb file. There is a description for this task somewhere? …Maybe I asked it in the past…
I want to learn about creating Python extensions.
please define the padding and width
sofar it makes no difference in the output if I use …:08X or …:X ?? but the first variant is slower!
@karolus thanks, updated to use that syntax.
@Zizi64: the older method is described in https://www.openoffice.org/udk/common/man/tools.html#idlc.
But I used the newer unoidl-write
: from the SDK command prompt, I run
unoidl-write "C:\Program Files\LibreOffice\program\types.rdb" D:\pyFloatToHex\XPyFloatToHex.idl D:\pyFloatToHex\XPyFloatToHex.rdb
The advantage of the macro is its self-containment
And for completeness, the third practical option listed in @erAck 's answer. We place in the document (for example, using APSO) the utils.py module containing the functions from @karolus 's answer.
To the Standard library, the Basic module:
Option Explicit
Global oScriptDTH As Object
' Returns a string of 16 hexadecimal digits corresponding to
' the internal representation of double.
Function DoubleToHex(ByVal d As Double) As String
If oScriptDTH Is Nothing Then
oScriptDTH = ThisComponent.getScriptProvider.getScript("vnd.sun.star.script:utils.py$doub_hex?language=Python&location=document")
End If
DoubleToHex=oScriptDTH.invoke(Array(d), array(), array())
End Function
thanks, updated to use that syntax
sorry…one more suggestion:
…
@staticmethod # with staticmethod …
def FloatToIEEE754Hex( f ): # … no ***self***argument